我是hibernate中的新蜜蜂,并使用注释创建实体。我正在使用onetomany我的xml看起来像这样
Answer.xml
<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类
@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;
}
这里的问题是AnswerText(子)类中没有Answer(父)对象声明。他们只声明了fileds.So我怎么能在
中正确注释private Set<AnswerText> answerText = new HashSet<AnswerText>();.
请帮助我。非常感谢。