Embeddable , Hibernate 中的多对一关系 [英] Many To One relation inside Embeddable , Hibernate
问题描述
我有一个场景,hibernate 中的 Embeddable
类使用 Entity
.根据我在 SO 和其他链接上找到的各种答案,我们可以在 Embeddable
类中编写 @ManyToOne、@OneToMany
.
I have a scenario where an Embeddable
class in hibernate uses anEntity
. According to various answers I found on SO and other links, we can write @ManyToOne, @OneToMany
inside an Embeddable
class.
但是这样做给了我 HibernateMappingExeption
考虑以下示例:我有两个实体和一个 Embeddable 类,如下所示:
Consider the following example: I have two Entities and an Embeddable class as under:
实体 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;
实体 B
@Entity
@Table(name = "B")
public class B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int b_id;
使用实体 B 的可嵌入类
@Embeddable
public class EmbeddableClass {
@ManyToOne
@JoinColumn(name = "b_id")
private B b;
我得到的错误如下:
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>
这篇关于Embeddable , Hibernate 中的多对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!