我正在尝试使这种模型休眠persist
方法:
package com.neo.jpa;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity
@Table(name="scoring_response"
)
public class Response {
@Id
@SequenceGenerator(name="scoring_credit_response_sequence" , sequenceName="scoring_credit_response_sequence")
@GeneratedValue(generator="scoring_credit_response_sequence")
private Long id;
@JoinColumn(name="dossier_reference")
@ManyToOne(fetch=FetchType.LAZY , optional=false)
@NotNull
private Dossier reference;
@ManyToOne(fetch=FetchType.LAZY , optional=false , cascade= {CascadeType.DETACH})
@JoinColumn(name="question_code")
@NotNull
private Question question;
@Column(name="response" , columnDefinition="text")
private String response;
public Response() {}
public Response(String jsonString) {
System.out.println(jsonString);
}
//getters and setters
}
Dossier
和Question
是简单的键值模型。
当我尝试使用保存的问题ID(数据库中存在该ID)持久保存新的Response
实体时,一切正常。
但是当我尝试使用未保存的问题ID来持久存储新的Response
时,此行为很好,我得到了以下错误跟踪:
"Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class org.apache.deltaspike.data.api.QueryInvocationException,message=Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class org.apache.deltaspike.data.api.QueryInvocationException,message=Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class java.lang.reflect.InvocationTargetException,message=null",
"Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class org.apache.deltaspike.data.api.QueryInvocationException,message=Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class java.lang.reflect.InvocationTargetException,message=null",
"Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class java.lang.reflect.InvocationTargetException,message=null",
null,
"org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.neo.jpa.Response.question -> com.neo.jpa.Question",
"Not-null property references a transient value - transient instance must be saved before current operation: com.neo.jpa.Response.question -> com.neo.jpa.Question"
但是,无论问题ID是否保存,所有下一个请求都将失败。我不知道为什么:
"Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class org.apache.deltaspike.data.api.QueryInvocationException,message=Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class org.apache.deltaspike.data.api.QueryInvocationException,message=Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class java.lang.reflect.InvocationTargetException,message=null",
"Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class org.apache.deltaspike.data.api.QueryInvocationException,message=Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class java.lang.reflect.InvocationTargetException,message=null",
"Failed calling Repository: [Repository=com.neo.repository.ResponseRepository,entity=com.neo.jpa.Response,method=persist,exception=class java.lang.reflect.InvocationTargetException,message=null",
null,
"There are delayed insert actions before operation as cascade level 0."
]
}
请,我想知道是什么问题。
注意:当我使用休眠save
方法时,一切都很好。