Hibernate One-To-One映射问题

时间:2016-08-12 16:09:21

标签: java hibernate

我有以下两个类:

public class User {

    private int id;
    private String name;
    private ProteinData proteinData;

    public User(){
        setProteinData(new ProteinData());
    }

    public ProteinData getProteinData() {
        return proteinData;
    }
    public void setProteinData(ProteinData proteinData_) {
        this.proteinData = proteinData_;
        proteinData_.setUser(this);
    }

   // additional getters and setters

}


public class ProteinData {

    private int id;
    private User user;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}

映射文件是:

//User
<hibernate-mapping>
   <class name="com.simpleprogrammer.User" table="USERS">
      <meta attribute="class-description">
         This class contains the User detail. 
      </meta>
      <id name="id" type="int">
         <column name="ID" />
         <generator class="identity"/>
      </id>
      <property name="name" type="java.lang.String">
         <column name="NAME" />
      </property>

      <one-to-one name="proteinData" class="com.simpleprogrammer.ProteinData" cascade="save-update"/>
</hibernate-mapping>

// proteinData
<hibernate-mapping>
   <class name="com.simpleprogrammer.ProteinData" table="PROTEINDATA">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="foreign">
                <param name="property">user</param>
            </generator>
        </id>
        <one-to-one name="user" class="com.simpleprogrammer.User" constrained="true">
        </one-to-one>
        <property name="total" type="int" column="TOTAL" />
        <property name="goal" type="int">
            <column name="GOAL"/>
        </property>
   </class>
</hibernate-mapping>

现在,当我运行我的应用程序时,我收到以下错误:

Exception in thread "main" org.hibernate.PropertyAccessException: Exception occurred inside setter of com.simpleprogrammer.User.proteinData
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:88)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:710)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:371)
    at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4509)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:186)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:137)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1108)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:964)
    at org.hibernate.loader.Loader.doQuery(Loader.java:911)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:312)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2121)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3927)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:460)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:429)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:206)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:145)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1092)
    at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:1007)
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:173)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:285)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
    at com.simpleprogrammer.User_$$_javassist_0.getName(User_$$_javassist_0.java)
    at com.pluralsight.Introduction_To_Hibernate_Pluralsight.Program.main(Program.java:45)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
    ... 25 more
Caused by: java.lang.NullPointerException
    at com.simpleprogrammer.User.setProteinData(User.java:44)
    ... 30 more

错误来自setProteinData()类中的User方法。错误中的最后一个语句显示java.lang.NullPointerException并指向该方法。我不知道我做错了什么。

2 个答案:

答案 0 :(得分:0)

我认为问题是你的ProteinData类中没有空(无参数)构造函数。尝试将其添加到课程中:

public ProteinData(){}

答案 1 :(得分:0)

proteinData_.setUser(this);方法移除setProteinData的来电。这不是必需的。

此外,您可以使用no-arg构造函数public ProteinData(){},或者即使您不使用no-arg构造函数,也没关系,如果没有其他构造函数,Compiler将创建默认构造函数-arg或带有参数的构造函数)存在于您的类中。

您可以在hibernate here中阅读有关no-arg构造函数要求的更多讨论。