我可以翻译以下内容:“我有一个需要拥有两个相同类型对象的类。我正在使用Hibernate,它会根据我的类创建数据库架构。”
“属性:”
当我把它改成
在这个类中,我该如何拥有两个相同类型的对象?谢谢!
“属性:”
private User user;
private User keyAccountManager;
Getter/Setter:
@ManyToOne
@JoinColumn(name = "userId")
@ForeignKey(name = "license_users_fk")
public User getUser() {
return user;
}
@ManyToOne
@JoinColumn(name = "userId")
@ForeignKey(name = "license_kam_fk")
public User getKeyAccountManager() {
return keyAccountManager;
}
如果我这样做,会出现以下错误:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: License column: userId (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:670)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:692)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:714)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468)
at org.hibernate.mapping.RootClass.validate(RootClass.java:215)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1135)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1320)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
... 55 more
当我把它改成
@JoinColumn(name = "userId", insertable=false, updatable=false)
时,它可以顺利部署,列已在数据库中创建,但是当我调用setKeyAccountManager()时,值没有设置到数据库中。在这个类中,我该如何拥有两个相同类型的对象?谢谢!