web-dev-qa-db-ja.com

Hibernate:nullの一対一プロパティからIDを割り当てようとしました:employee

これは私のデータベース構造、MySQLの1対1マッピングです。

DB Schema

これは私のJavaファイル:

public class Employee {

private EmployeeDetail empdetail;
private String firstname;
private String lastname;

// getters and setters 

}


public class EmployeeDetail {
    private Employee employee ;
    private int employee_id;
    private String street;
    private String city;
    private String state;
    private String country;

// getters and setters 

}

これは私のマッピングファイルです。

<hibernate-mapping>

     <class name="Employee" table="employee">
        <id name="employee_id"  type="Java.lang.Integer"  column="employee_id">
            <generator class="assigned" />
        </id>
        <one-to-one name="empdetail" class="EmployeeDetail"
            cascade="all"></one-to-one>

        <property name="firstname" type="Java.lang.String" column="firstname" />
        <property name="lastname" type="Java.lang.String" column="lastname" />

    </class>



 <class name="EmployeeDetail" table="employeedetail">
        <id name="employee_id" type="Java.lang.Integer"  column="employee_id">
       <generator class="foreign">
                <param name="property">employee</param>
            </generator>
        </id>
        <one-to-one name="employee" class="Employee"
            cascade="save-update"></one-to-one>

        <property name="street"  type="Java.lang.String" column="street" />
        <property name="city" type="Java.lang.String" column="city" />
        <property name="state" type="Java.lang.String" column="state" />
        <property name="country" type="Java.lang.String" column="country" />

 </class>

</hibernate-mapping>

これは私のクライアントプログラムです。

Session session = factory.openSession();
        Transaction tx = session.beginTransaction();

        EmployeeDetail edetail = new EmployeeDetail();
        edetail.setCity("Hyd");
        edetail.setCountry("India");
        edetail.setEmployee_id(222);
        edetail.setState("Andhra Pradesh");

        Employee employee = new Employee();

        employee.setEmployee_id(222);
        employee.setFirstname("Pavan");
        employee.setLastname("Jaooi");
        employee.setEmpdetail(edetail);

        session.merge(employee);
        tx.commit();
        session.close();
        factory.close();

これは私が得ている例外です:

INFO: Not binding factory to JNDI, no JNDI name configured
Hibernate: select employee0_.employee_id as employee1_0_1_, employee0_.firstname as firstname0_1_, employee0_.lastname as lastname0_1_, employeede1_.employee_id as employee1_1_0_, employeede1_.street as street1_0_, employeede1_.city as city1_0_, employeede1_.state as state1_0_, employeede1_.country as country1_0_ from employee employee0_ left outer join employeedetail employeede1_ on employee0_.employee_id=employeede1_.employee_id where employee0_.employee_id=?
Hibernate: select employeede0_.employee_id as employee1_1_0_, employeede0_.street as street1_0_, employeede0_.city as city1_0_, employeede0_.state as state1_0_, employeede0_.country as country1_0_ from employeedetail employeede0_ where employeede0_.employee_id=?
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: employee
    at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.Java:44)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.Java:99)
    at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.Java:186)
    at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.Java:240)
    at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.Java:120)
    at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.Java:687)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.Java:669)
    at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.Java:245)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.Java:268)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.Java:216)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.Java:169)
    at org.hibernate.engine.Cascade.cascade(Cascade.Java:130)
    at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.Java:456)
    at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.Java:194)
    at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.Java:240)
    at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.Java:120)
    at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.Java:53)
    at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.Java:677)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.Java:661)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.Java:665)
    at CustomerClient.main(CustomerClient.Java:31)
29
Pawan

employeeプロパティのIDからEmployeeDetail IDを生成するようにHibernateに指示しましたが、このプロパティを初期化したことはありません。

edetail.setEmployee(employee);をコードに追加します。

52
JB Nizet