我想坚持我的QuestionCompletion类的对象与所有子元素.其中一个孩子有一个复合主键.作为这个主键的一部分,我也有另一个实体的外键.结果我得到这个错误:
Exception caught during request processing: javax.ejb.EJBTransactionRolledbackException:
could not set a field value by reflection setter of com.example.model.domain.QuestionCompletionAnswerPK.questionCompletionId
javax.ejb.EJBTransactionRolledbackException: could not set a field value by reflection
setter of com.example.model.domain.QuestionCompletionAnswerPK.questionCompletionId
最后一个“由…引起”当然是NullPointerException:
Caused by: java.lang.NullPointerException
QuestionCompletion questionCompletion = new QuestionCompletion();
List
这是我的基本实体类,我想坚持其所有的子元素.我已经意识到List< QuestionCompletionAnswer>引起问题.我使用cascade = CascadeType.ALL来允许持久化子元素.
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "question_completion")
public class QuestionCompletion implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "question_completion_gen")
@SequenceGenerator(name = "question_completion_gen",sequenceName = "question_completion_id_seq")
@Column(name = "question_completion_id")
private Long id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "extended_question_id")
protected List
这是我的类 – QuestionCompletionAnswer类的主键.
@Embeddable
public class QuestionCompletionAnswerPK implements Serializable {
@Column(name = "question_completion_id")
protected Long questionCompletionId;
@Column(name = "extended_question_id")
protected Long extendedQuestionId;
}
这是使用我的EmbeddedId的类.属性问题ComplempleId和questionCompletionId是某些其他实体的外键,所以我在下面还用@MapsId注释放置了这些实体的整个对象.
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "extended_question_answer")
public class QuestionCompletionAnswer implements Serializable {
@EmbeddedId
private QuestionCompletionAnswerPK id;
@ManyToOne
@MapsId(value = "questionCompletionId")
@JoinColumn(name = "question_completion_id",nullable = false,insertable = false,updatable = false)
protected QuestionCompletion questionCompletion;
@ManyToOne
@MapsId(value = "extendedQuestionId")
@JoinColumn(name = "extended_question_id",updatable = false)
protected ExtendedQuestion extendedQuestion;
}
你能告诉我我的注释是否正确吗?也许我混淆了一些方法.或者我不能在这种情况下坚持我的基本对象及其所有子元素.
编辑
现在我的映射看起来像:
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "question_completion")
public class QuestionCompletion implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,sequenceName = "question_completion_id_seq")
@Column(name = "question_completion_id")
private Long id;
@OneToMany(cascade = CascadeType.ALL,mappedBy = "questionCompletion")
protected List
QuestionCompletionAnswerPK类的代码是相同的.
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "extended_question_answer")
public class QuestionCompletionAnswer implements Serializable {
@EmbeddedId
private QuestionCompletionAnswerPK id;
@ManyToOne(cascade = CascadeType.PERSIST)
@MapsId(value = "questionCompletionId")
@JoinColumn(name = "question_completion_id",nullable = false)
protected QuestionCompletion questionCompletion;
@ManyToOne
@MapsId(value = "extendedQuestionId")
@JoinColumn(name = "extended_question_id",nullable = false)
protected ExtendedQuestion extendedQuestion;
}
通过该映射,我仍然得到相同的异常.
编辑#2
但是,当我以这种方式更改QuestionCompletionAnswer类时:
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "extended_question_answer")
public class QuestionCompletionAnswer implements Serializable {
@EmbeddedId
private QuestionCompletionAnswerPK id;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "question_completion_id",updatable = false)
protected QuestionCompletion questionCompletion;
@ManyToOne
@JoinColumn(name = "extended_question_id",updatable = false)
protected ExtendedQuestion extendedQuestion;
}
我得到了那个例外:
Caused by: org.hibernate.id.IdentifierGenerationException: null id generated
for:class com.example.model.domain.QuestionCompletionAnswer
最佳答案