我实际上正在学习hibernate,我认为@Embbebed和@Embbedable它不适合我
我明白了:
@Entity
public class Poi {
@Id @GeneratedValue
private long id;
@Embedded
private PoiType poiType;
private String name;
@SuppressWarnings("unused")
private Poi(){}
public Poi(PoiType type,String name) {
this.poiType = type;
this.name = name;
}
@Embeddable
public class TypeA extends PoiType {
private Integer number;
@SuppressWarnings("unused")
private TypeA(){}
public TypeA(Integer num) {
this.number=num;
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="1.0">
<persistence-unit name="db" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>tpAnual.POIs.Poi</class>
<class>tpAnual.POIs.PoiType</class>
<class>tpAnual.POIs.TypeA</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:app-db" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="use_sql_comments" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
当我运行测试(一切设置正确)时,控制台只打印:
休眠: 插入 到 POI (id,name) 值 (默认,?)
我错过了什么?非常感谢你!
答案 0 :(得分:0)
嵌入实体中的继承几乎没有意义,因为它根据嵌入实体的实际类在包含实体表中调用不同的列集,但在声明时,ORM对此没有任何了解。可以从要嵌入的基类派生的类。我听说EclipseLink以某种方式做到了,(我不想甚至想到他们用什么样的邪恶行为来推动这个技巧),但据我所知,hibernate并不支持它。