Unidirection OnetoOne映射外部主键未在子表

时间:2015-06-09 18:52:36

标签: java hibernate hibernate-mapping one-to-one hbmxml

我有一个父表CUSTOMER_ACCOUNT和一个子表CUSTOMER_TEMPINFO。

我试图在父表中生成主键并在子表中分配相同的主键。

我使用单向方式,所以双向解决方案请直到绝对必要。

关联订单中有什么问题!

需要离开孩子 - >家长吗?

需要一些线索和正确的解释!

以下是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

即使在考虑了双向方式之后,在阅读相同的背部时能够插入记录而不是幸运。这一次有了以下痕迹 -

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]

2 个答案:

答案 0 :(得分:1)

好的,我现在明白了。拇指规则是一对一的映射,你不能让hibernate分配子实体的PK。在执行saveOrUpdate之后,我不得不在子代中单独分配PK。

以下是我所做的更改。

删除了property-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

答案 1 :(得分:0)

不幸的是,您必须通过必需的属性property指定要从哪个关联实体获取ID值,以初始化外部生成器。所以你需要至少添加从子到父的关联。像这样:

<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>

一个有用的链接:see 5.1.2.2.7. Identity copy (foreign generator)