我有一个场景,其中hibernate中的Embeddable
类使用Entity
。根据我在SO和其他链接上找到的各种答案,我们可以在@ManyToOne, @OneToMany
类中编写Embeddable
。
但这样做会给我HibernateMappingExeption
考虑以下示例: 我有两个实体和一个Embeddable类,如下所示:
实体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)]
有人可以建议我是否正确使用这些东西,如果是的话,我错过了什么?
答案 0 :(得分:0)
假设您的方案我尝试了以下操作,我没有遇到任何问题:
@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-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>