在Hibernate注释中获取错误java.lang.NullPointerException

时间:2016-07-20 18:14:02

标签: java spring hibernate annotations

我正在将XML文件转换为Java中的注释。因为我根据XML使用了下面的注释代码,我们使用了集合概念。

这与处理null指针异常相关的任何其他帖子无关,这是在我正在寻求帮助的带注释的代码中。

ANSWER表:

 <hibernate-mapping>
        <class name="com.sample.Answer"
             table="ANSWERS">
             <cache usage="read-write"/>
        <id name="answerId" type="java.lang.Long">
               <column name="ANSWER_ID"/>
              <generator class="identity"/>
        </id>

          <property name="questionId" type="java.lang.Long">
            <column name="QUESTION_ID"/>
             </property>

        <set name="answerText" table="ANSWERTEXT"
                inverse="true"  fetch="select" lazy="false">
            <key>
                <column name="ANSWER_ID" not-null="true" />
            </key>
            <one-to-many class="com.sample.AnswerText" />
        </set>
          </class>
    </hibernate-mapping>

带注释的代码:

@Entity
@Table(name = "ANSWERS")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

@JsonIgnoreProperties(ignoreUnknown = true)
public class Answer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ANSWER_ID")
    private Long  answerId;

    @Column(name = "QUESTION_ID")
    private Long      questionId;

    @OneToMany
    @Fetch(FetchMode.SELECT)
    private Set<AnswerText> answerText = new HashSet<AnswerText>();

    }

ANSWERTEXT表:

 <hibernate-mapping>
        <class name="com.sample.AnswerText"  table="ANSWERTEXT">
             <cache usage="read-write"/>
          <id name="answerTextId" type="java.lang.Long">
               <column name="ANSWER_TEXT_ID"/>
              <generator class="identity"/>
          </id>
          <property name="answerId" type="java.lang.Long">
            <column name="ANSWER_ID"/>
             </property>
          <property name="answerText" type="java.lang.String">
            <column name="ANSWER_TEXT"/>
          </class>
    </hibernate-mapping>

带注释的代码:

    @Entity
@Table(name = "ANSWERTEXT")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

@JsonIgnoreProperties(ignoreUnknown = true)
public class AnswerText {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ANSWER_TEXT_ID")
    private Long answerTextId;

    @Column(name = "ANSWER_ID")
    private Long answerId;

    @Column(name = "ANSWER_TEXT")
    private String answerText;

    }

任何人都可以帮我解释我在注释文件中的错误是什么,所以我得到null指针异常。请帮助我这个我是Java和hibernate的新手。

0 个答案:

没有答案