我有一个问题,我有两个实体Job和JobPK

Job class looks like this sample code :

@Entity
@IdClass(JobPK.class)
@Table(name="JOB")
@Inheritance
@DiscriminatorColumn(name="JOB_TYPE")

public abstract class Job implements Serializable {
@Id
@Column(name="FOLDER_ID")
private BigDecimal folderId;

@Id
@ColumnDefinition(position = 1)
private String name;

@Column(name="JOB_TYPE",insertable=false,updatable=false)
private String jobType;
...
}


和JobPk:

public class JobPK implements Serializable {

private static final long serialVersionUID = -3266336718203527905L;

@Column(name="JOB_TYPE",insertable=false,updatable=false)
private String jobType;

@Id
private String name;

@Id
@Column(name="FOLDER_ID")
private BigDecimal folderId;
......
}


我有两个扩展Job的类:CalculatingJob和ImportingJob
现在我不会使用:

getEntityManager().find(CalculatingJob.class, new JobPK    (BigDecimal.valueOf(folderId),name))


我有问题,因为我必须填写JobPK描述符值字段。如果我不这样做,我会得到Null Pointer Exception。我认为默认情况下,描述符值是关键,但我不希望在JobPk创建期间放置有关描述符值的信息。我认为从Job扩展的Entity会自动填充此字段。任何绕过此问题的想法,也许我都可以从CalculatingJob获取Annotation @DescriminatorVale,然后放入构造函数JobPk中

感谢帮助

最佳答案

尝试此配置用于层次结构

Job.java

@Table(name = "JOB")
@Inheritance
@IdClass(JobPK.class)
@DiscriminatorColumn(name = "JOB_TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class Job implements java.io.Serializable {

}


CalculatingJob.java

@Entity
@DiscriminatorValue("CalculatingJob")
public class CalculatingJob extends Job {

}


导入Job.java

@Entity
@DiscriminatorValue("ImportingJob")
public class ImportingJob extends Job {

}


JobPK.java

public class JobPK implements Serializable {

}


鉴别值是通过休眠输入的。

10-06 01:37