[英]Hibernate Entity Class maps to two different Tables
I am trying to Map my @Entity
objects to their respective classes where one is a parameter of the other. 我试图将我的@Entity
对象映射到它们各自的类,其中一个是另一个的参数。
To put it simply I have something like this: 简而言之,我有这样的事情:
@Entity
@Table(name="TableA")
public class ClassA {
@Id
private long id;
private String paramA;
private ClassB classB;
// getters and setters here
}
ClassB looks like: ClassB看起来像:
@Entity
@Table(name="TableB")
public class ClassB {
@Id
private long classAId;
private String paramB;
// getters and setters here
}
To save I am using the Interface - (I suspect this or the way I am using it is my problem?) 为了保存我正在使用界面 - (我怀疑这个或我使用它的方式是我的问题?)
@Transactional
public interface ClassADao extends JpaRepository<ClassA, Integer> {
}
In my DB all parameters in ClassA map to a corresponding table except for ClassB which has parameters that all match to a different table for ClassB. 在我的DB中,ClassA中的所有参数都映射到相应的表,但ClassB除外,ClassB的参数都与ClassB的不同表匹配。 I'm new to Hibernate and was hoping it would map the params of ClassB to the correct table. 我是Hibernate的新手,希望它能将ClassB的参数映射到正确的表格。 However it appears to be trying to map ClassB to a column in the table for ClassA and thus giving me this error: 但是它似乎试图将ClassB映射到表中的ClassA列,从而给出了这个错误:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'classB' in 'field list'
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:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
My question is, is there a way (preferably through Annotation) to tell Hibernate to make the params in ClassB map to it's own table? 我的问题是,是否有一种方法(最好通过Annotation)告诉Hibernate使ClassB中的参数映射到它自己的表? I have tried using @SecondaryTable but that didn't work. 我尝试过使用@SecondaryTable但是没有用。
Thanks in advance! 提前致谢!
With help from the people who commented on this, I was able to come up with the following solution: 在评论这一点的人的帮助下,我能够提出以下解决方案:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@OneToOne(cascade = {CascadeType.ALL})
@PrimaryKeyJoinColumn
private ClassB classB;
public setClassB(ClassB classB) {
this.classB = classB;
this.classB.setClassA(this);
}
And edited ClassB like so: 并编辑了ClassB,如下所示:
@Entity
@Table(name="TableB")
public class ClassB {
@MapsId
@OneToOne
@JoinColumn
private ClassA classA
private String paramB;
// getters and setters here - including for ClassA
} }
Now when I call the Spring injected ClassADao.save(classA)
, ClassB
also gets saved in the DB correctly for free. 现在,当我调用Spring注入的ClassADao.save(classA)
, ClassB
也可以正确地保存在DB中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.