我在Hibernate中创建一对一映射时遇到问题。以下是我试图实现它的方式。
以下是我的SysEntity超级课程
@MappedSuperclass
public class BaseSysEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name="sysupdate")
private Date sysupdate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getSysupdate() {
return sysupdate;
}
public void setSysupdate(Date sysupdate) {
this.sysupdate = sysupdate;
}
}
以下类是将与“Project”
创建oneToOne关系的实体@Entity
@Table(name="sysproject")
public class SysProject extends BaseSysEntity implements Serializable {
@OneToOne(optional=true, fetch= FetchType.LAZY)
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name="sysClientId", referencedColumnName="sysClientId"),
@PrimaryKeyJoinColumn(name="pProject", referencedColumnName="pProject")
})
private Project project;
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
}
以下是Project类:
@Entity
@Table(name = "tproject")
public class Project {
@EmbeddedId
private ProjectID id; // It contains sysClientId and pProject as primary key
@OneToOne(optional=true, mappedBy="project")
private SysProject SysProject;
}
ProjectID类:
@Embeddable
public class ProjectID implements Serializable{
@Column(name="pProject")
private String project;
@Column(name="sysClientId")
private String sysClientId;
public String getProject() {
return project;
}
public ProjectID(){
this.sysClientId="0";
}
public ProjectID(Integer number){
this();
this.project = number.toString();
}
public void setProject(String project) {
this.project = project;
}
public String getSysClientId() {
return sysClientId;
}
public void setSysClientId(String sysClientId) {
this.sysClientId = sysClientId;
}
}
我得到的例外:
Caused by: org.hibernate.MappingException: broken column mapping for: SysProject.id of: com.spin.integration.dto.Project
at org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:178)
at org.hibernate.persister.entity.AbstractPropertyMapping.initIdentifierPropertyPaths(AbstractPropertyMapping.java:249)
at org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:222)
at org.hibernate.persister.entity.AbstractEntityPersister.initOrdinaryPropertyPaths(AbstractEntityPersister.java:2434)
at org.hibernate.persister.entity.AbstractEntityPersister.initPropertyPaths(AbstractEntityPersister.java:2471)
at org.hibernate.persister.entity.AbstractEntityPersister.postConstruct(AbstractEntityPersister.java:3766)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:451)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:386)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1790)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
... 18 more
注意:此oneToOne映射不存在于数据库级别(不幸的是我无法对数据库进行更改)。
答案 0 :(得分:2)
您可以检查此解决方案的工作原理: One Id of composite key to be referenced in another entity 他面临同样的问题
@OneToOne(optional=true, fetch= FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="sysClientId", referencedColumnName="sysClientId"),
@JoinColumn(name="pProject", referencedColumnName="pProject")
})
答案 1 :(得分:0)
我必须使用@JoinColumns而不是@PrimaryKeyJoinColumns。