[英]Many To One relation inside Embeddable , Hibernate
I have a scenario where an Embeddable
class in hibernate uses an Entity
. 我有一个场景,其中hibernate中的Embeddable
类使用Entity
。 According to various answers I found on SO and other links, we can write @ManyToOne, @OneToMany
inside an Embeddable
class. 根据不同的答案,我发现了SO等各个环节,我们可以写@ManyToOne, @OneToMany
内部Embeddable
类。
But doing this gives me HibernateMappingExeption
但这样做给了我HibernateMappingExeption
Consider the following example: I have two Entities and an Embeddable class as under: 请考虑以下示例:我有两个实体和一个Embeddable类,如下所示:
Entity A 实体A.
@Entity
@Table(name = "A")
public class A {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int a_id;
@ElementCollection
@JoinTable(name = "embeded_class_table", joinColumns = @JoinColumn(name = "a_id"))
private List<EmbeddedClass> embeddedClass;
Entity B 实体B.
@Entity
@Table(name = "B")
public class B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int b_id;
Embeddable Class which uses Entity B 使用实体B的可嵌入类
@Embeddable
public class EmbeddableClass {
@ManyToOne
@JoinColumn(name = "b_id")
private B b;
The error I am getting is as under: 我得到的错误如下:
org.hibernate.MappingException: Could not determine type for: app.model.B, at table: embeded_class_table, for columns: [org.hibernate.mapping.Column(b)]
Could anyone please suggest if I am using these stuff correctly and if yes, what am I missing? 任何人都可以建议我是否正确使用这些东西,如果是的话,我错过了什么?
Assuming your scenario I tried the following I didn't get any issues: 假设您的方案我尝试了以下内容,我没有遇到任何问题:
@Entity
@Table(name = "A")
public class A {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int a_id;
@ElementCollection
@JoinTable(name = "embeded_class_table", joinColumns = @JoinColumn(name = "a_id"))
private List<EmbeddableClass> embeddedClass;
public int getA_id() {
return a_id;
}
public void setA_id(int a_id) {
this.a_id = a_id;
}
public List<EmbeddableClass> getEmbeddedClass() {
return embeddedClass;
}
public void setEmbeddedClass(List<EmbeddableClass> embeddedClass) {
this.embeddedClass = embeddedClass;
}
}
@Entity
@Table(name = "B")
public class B {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int b_id;
}
@Embeddable
public class EmbeddableClass {
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "b_id")
private B b;
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
hibernate.cfg.xml 的hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">xxxx</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">elias</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="show_sql">true</property>
<mapping class="com.springex.dto.A"></mapping>
<mapping class="com.springex.dto.B"></mapping>
<mapping class="com.springex.dto.EmbeddableClass"></mapping>
</session-factory>
</hibernate-configuration>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.