child table 中未生成单向一对一映射外主键
Unidirection OnetoOne mapping foreign primary key is not generating in child table
我有一个parenttableCUSTOMER_ACCOUNT和一个childtableCUSTOMER_TEMPINFO。
我正在尝试在 parent table 中生成主键并在 child table 中分配相同的主键。
我必须使用单向方式,所以在绝对必要之前请使用双向解决方案。
是不是关联顺序有问题!
需要从 child -> parent?
需要一些线索和适当的解释!
这是 hbm 映射。
CustomerAccount.hbm.xml
<hibernate-mapping>
<class name="com.rup.example.po.CustomerAccount" table="CUSTOMER_ACCOUNT">
<id name="customerId" type="int" column="customer_id">
<generator class="native" />
</id>
<property name="customerName" column="customer_name" type="string" />
<property name="birthDay" column="birthday" type="date" />
<one-to-one name="tempInfo"
class="com.rup.example.po.CustomerTempInfo"
property-ref="customerId"
cascade="all" />
</class>
</hibernate-mapping>
CustomerTempInfo.hbm.xml
<hibernate-mapping>
<class name="com.rup.example.po.CustomerTempInfo" table="CUSTOMER_TEMPINFO">
<id name="customerId" type="int" column="customer_id">
<generator class="foreign">
</generator>
</id>
<property name="passCode" column="pass_code" type="string" />
<property name="codeExpiryDate" column="code_expiry_date" type="date" />
</class>
</hibernate-mapping>
我得到以下跟踪:-
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.rup.example.po.CustomerTempInfo]
org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.rup.example.po.CustomerTempInfo]
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:123)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:191)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:305)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at com.rup.example.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:24)
at com.rup.example.util.HibernateUtil.getSessionFactory(HibernateUtil.java:36)
at com.rup.example.main.TestMainApp.main(TestMainApp.java:21)
Caused by: org.hibernate.MappingException: param named "property" is required for foreign id generation strategy
at org.hibernate.id.ForeignGenerator.configure(ForeignGenerator.java:88)
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:117)
... 6 more
Exception in thread "main" java.lang.NullPointerException
at com.rup.example.main.TestMainApp.main(TestMainApp.java:40)
更新 20150610
即使考虑了Bi-directional方式,也能插入记录,但在读取相同的返回时并不幸运。这次得到了以下痕迹 -
Session created
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into CUSTOMER_TEMPINFO (pass_code, code_expiry_date, customer_id) values (?, ?, ?)
Hibernate: insert into CUSTOMER_ACCOUNT (customer_name, birthday, customer_id) values (?, ?, ?)
Customer ID=22
Hibernate: select customerac0_.customer_id as customer_id1_0_0_, customerac0_.customer_name as customer_name2_0_0_, customerac0_.birthday as birthday3_0_0_ from CUSTOMER_ACCOUNT customerac0_ where customerac0_.customer_id=?
Exception occured. null
java.lang.NullPointerException
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2385)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:767)
at org.hibernate.type.EntityType.resolve(EntityType.java:505)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:244)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:215)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:140)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:146)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:976)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:174)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
at com.rup.example.po.CustomerAccount_$$_jvst751_1.toString(CustomerAccount_$$_jvst751_1.java)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.rup.example.main.TestMainApp.printTransactionData(TestMainApp.java:65)
at com.rup.example.main.TestMainApp.main(TestMainApp.java:38)
Closing SessionFactory
Jun 10, 2015 11:55:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:oracle:thin:@localhost:1521:xe]
不幸的是,您必须指定要从哪个关联实体获取 ID 值,通过所需的 属性 property
来初始化外部生成器。所以你至少需要添加从child到parent的关联。像这样:
<hibernate-mapping>
<class name="com.rup.example.po.CustomerTempInfo" table="CUSTOMER_TEMPINFO">
<id name="customerId" type="int" column="customer_id">
<generator class="foreign">
<param name="property">parent</param>
</generator>
</id>
<property name="passCode" column="pass_code" type="string" />
<property name="codeExpiryDate" column="code_expiry_date" type="date" />
<one-to-one name="parent" class="com.rup.example.po.CustomerAccount" constrained="true" />
</class>
</hibernate-mapping>
好的,我明白了。经验法则是一对一的映射,你不能让休眠来分配子实体的主键。在执行 saveOrUpdate 后,我不得不在子节点中单独分配 PK。
这是我所做的更改。
删除了 属性-ref
<one-to-one name="tempInfo"
class="com.rup.example.po.CustomerTempInfo"
cascade="all"/>
已分配发电机
<class name="com.rup.example.po.CustomerTempInfo" table="CUSTOMER_TEMPINFO">
<id name="customerId" type="int" column="customer_id">
<generator class="assigned"/>
</id>
<property name="passCode" column="pass_code" type="string" />
<property name="codeExpiryDate" column="code_expiry_date" type="timestamp" />
</class>
这是主要方法:-
CustomerAccount account = new CustomerAccount();
account.setBirthDay(new Date());
account.setCustomerName("Rup Majumder");
tx = session.beginTransaction();
session.saveOrUpdate(account);
CustomerTempInfo tempInfo = new CustomerTempInfo();
tempInfo.setCodeExpiryDate(new Date());
tempInfo.setPassCode("ABCD");
tempInfo.setCustomerId(account.getCustomerId());
account.setTempInfo(tempInfo);
tx.commit();
使用 4.0。1.Final
我有一个parenttableCUSTOMER_ACCOUNT和一个childtableCUSTOMER_TEMPINFO。
我正在尝试在 parent table 中生成主键并在 child table 中分配相同的主键。
我必须使用单向方式,所以在绝对必要之前请使用双向解决方案。
是不是关联顺序有问题!
需要从 child -> parent?
需要一些线索和适当的解释!
这是 hbm 映射。
CustomerAccount.hbm.xml
<hibernate-mapping>
<class name="com.rup.example.po.CustomerAccount" table="CUSTOMER_ACCOUNT">
<id name="customerId" type="int" column="customer_id">
<generator class="native" />
</id>
<property name="customerName" column="customer_name" type="string" />
<property name="birthDay" column="birthday" type="date" />
<one-to-one name="tempInfo"
class="com.rup.example.po.CustomerTempInfo"
property-ref="customerId"
cascade="all" />
</class>
</hibernate-mapping>
CustomerTempInfo.hbm.xml
<hibernate-mapping>
<class name="com.rup.example.po.CustomerTempInfo" table="CUSTOMER_TEMPINFO">
<id name="customerId" type="int" column="customer_id">
<generator class="foreign">
</generator>
</id>
<property name="passCode" column="pass_code" type="string" />
<property name="codeExpiryDate" column="code_expiry_date" type="date" />
</class>
</hibernate-mapping>
我得到以下跟踪:-
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.rup.example.po.CustomerTempInfo]
org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.rup.example.po.CustomerTempInfo]
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:123)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:191)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:305)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at com.rup.example.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:24)
at com.rup.example.util.HibernateUtil.getSessionFactory(HibernateUtil.java:36)
at com.rup.example.main.TestMainApp.main(TestMainApp.java:21)
Caused by: org.hibernate.MappingException: param named "property" is required for foreign id generation strategy
at org.hibernate.id.ForeignGenerator.configure(ForeignGenerator.java:88)
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:117)
... 6 more
Exception in thread "main" java.lang.NullPointerException
at com.rup.example.main.TestMainApp.main(TestMainApp.java:40)
更新 20150610
即使考虑了Bi-directional方式,也能插入记录,但在读取相同的返回时并不幸运。这次得到了以下痕迹 -
Session created
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into CUSTOMER_TEMPINFO (pass_code, code_expiry_date, customer_id) values (?, ?, ?)
Hibernate: insert into CUSTOMER_ACCOUNT (customer_name, birthday, customer_id) values (?, ?, ?)
Customer ID=22
Hibernate: select customerac0_.customer_id as customer_id1_0_0_, customerac0_.customer_name as customer_name2_0_0_, customerac0_.birthday as birthday3_0_0_ from CUSTOMER_ACCOUNT customerac0_ where customerac0_.customer_id=?
Exception occured. null
java.lang.NullPointerException
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2385)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:767)
at org.hibernate.type.EntityType.resolve(EntityType.java:505)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:244)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:215)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:140)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:146)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:976)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:174)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
at com.rup.example.po.CustomerAccount_$$_jvst751_1.toString(CustomerAccount_$$_jvst751_1.java)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.rup.example.main.TestMainApp.printTransactionData(TestMainApp.java:65)
at com.rup.example.main.TestMainApp.main(TestMainApp.java:38)
Closing SessionFactory
Jun 10, 2015 11:55:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:oracle:thin:@localhost:1521:xe]
不幸的是,您必须指定要从哪个关联实体获取 ID 值,通过所需的 属性 property
来初始化外部生成器。所以你至少需要添加从child到parent的关联。像这样:
<hibernate-mapping>
<class name="com.rup.example.po.CustomerTempInfo" table="CUSTOMER_TEMPINFO">
<id name="customerId" type="int" column="customer_id">
<generator class="foreign">
<param name="property">parent</param>
</generator>
</id>
<property name="passCode" column="pass_code" type="string" />
<property name="codeExpiryDate" column="code_expiry_date" type="date" />
<one-to-one name="parent" class="com.rup.example.po.CustomerAccount" constrained="true" />
</class>
</hibernate-mapping>
好的,我明白了。经验法则是一对一的映射,你不能让休眠来分配子实体的主键。在执行 saveOrUpdate 后,我不得不在子节点中单独分配 PK。
这是我所做的更改。
删除了 属性-ref
<one-to-one name="tempInfo"
class="com.rup.example.po.CustomerTempInfo"
cascade="all"/>
已分配发电机
<class name="com.rup.example.po.CustomerTempInfo" table="CUSTOMER_TEMPINFO">
<id name="customerId" type="int" column="customer_id">
<generator class="assigned"/>
</id>
<property name="passCode" column="pass_code" type="string" />
<property name="codeExpiryDate" column="code_expiry_date" type="timestamp" />
</class>
这是主要方法:-
CustomerAccount account = new CustomerAccount();
account.setBirthDay(new Date());
account.setCustomerName("Rup Majumder");
tx = session.beginTransaction();
session.saveOrUpdate(account);
CustomerTempInfo tempInfo = new CustomerTempInfo();
tempInfo.setCodeExpiryDate(new Date());
tempInfo.setPassCode("ABCD");
tempInfo.setCustomerId(account.getCustomerId());
account.setTempInfo(tempInfo);
tx.commit();
使用 4.0。1.Final