如何避免TransientPropertyValueException

时间:2017-07-09 16:55:16

标签: java hibernate

我有班级Anagrafica(帐户,英文),我需要从表格ID_Tipo_Anagrafica(帐户类型,英文)中设置。我创建了一个可以获取ID_Tipo_Anagrafica列表的表单,但我无法创建最终对象以便将其放入数据库中。

anagrafica类

public class Anagrafica {
    private int ID_Anagrafica;
    private int ID_Tipo_Anagrafica;
    private String Nome;
    private String Cognome;
    private String Provincia;
    private String Codice_fiscale;
    private Ruolo Ruolo;
    private Utenza utenza;
    private Tessera tessera;
    private Set<Scontrino> scontrini = new HashSet<>(0);

    public Anagrafica() {
        super();
    }

    public Anagrafica(String nome, String cognome, String provincia, String codice_fiscale, Ruolo ruolo) {
        super();
        this.Nome = nome;
        this.Cognome = cognome;
        this.Provincia = provincia;
        this.Codice_fiscale = codice_fiscale;
        this.Ruolo = ruolo;
    }

    public int getID_Anagrafica() {
        return ID_Anagrafica;
    }
    public void setID_Anagrafica(int iD_Anagrafica) {
        ID_Anagrafica = iD_Anagrafica;
    }
    public String getNome() {
        return Nome;
    }
    public void setNome(String nome) {
        Nome = nome;
    }
    public String getCognome() {
        return Cognome;
    }
    public void setCognome(String cognome) {
        Cognome = cognome;
    }
    public String getProvincia() {
        return Provincia;
    }
    public void setProvincia(String provincia) {
        Provincia = provincia;
    }
    public String getCodice_fiscale() {
        return Codice_fiscale;
    }
    public void setCodice_fiscale(String codice_fiscale) {
        Codice_fiscale = codice_fiscale;
    }
    public Ruolo getRuolo() {
        return Ruolo;
    }
    public void setRuolo(Ruolo ruolo) {
        Ruolo = ruolo;
    }
    public Utenza getUtenza() {
        return utenza;
    }
    public void setUtenza(Utenza utenza) {
        this.utenza = utenza;
    }


    public Set<Scontrino> getScontrini() {
        return scontrini;
    }

    public void setScontrini (Set<Scontrino> scontrini) {
        this.scontrini = scontrini;
    }



    public Tessera getTessera() {
        return tessera;
    }

    public void setTessera(Tessera tessera) {
        this.tessera = tessera;
    }


    public int getID_Tipo_Anagrafica() {
        return ID_Tipo_Anagrafica;
    }

    public void setID_Tipo_Anagrafica(int ID_Tipo_Anagrafica) {
        this.ID_Tipo_Anagrafica = ID_Tipo_Anagrafica;
    }

}

anagrafica行动

public class AnagraficaStruts extends ActionSupport implements UserAware, ModelDriven<Anagrafica> {
    private static final long serialVersionUID = 1L;

    private Anagrafica anagrafica = new Anagrafica();
    private AnagraficaDAO dao = AnagraficaDAOFactory.getDAO();
    private List<Anagrafica> anagrafiche = null;

    private List<Ruolo> ruoli = null;
    private RuoloDAO ruoloDAO = RuoloDAOFactory.getDAO();


    public List<Ruolo> getRuoli() {
        return ruoli;
    }

    public void setRuoli(List<Ruolo> ruoli) {
        this.ruoli = ruoli;
    }

    public Anagrafica getAnagrafica() {
        return anagrafica;
    }

    public void setAnagrafica(Anagrafica anagrafica) {
        this.anagrafica = anagrafica;
    }

    public List<Anagrafica> getAnagrafiche() {
        return anagrafiche;
    }

    public void setAnagrafiche(List<Anagrafica> anagrafiche) {
        this.anagrafiche = anagrafiche;
    }

    public String listAnagrafiche(){
        ruoli = new ArrayList<Ruolo>();
        setAnagrafiche(dao.getAnagrafiche());
        for (int i=0; i<anagrafiche.size(); i++){
            anagrafica = anagrafiche.get(i);
            ruoli.add(anagrafica.getRuolo());
        }
        return SUCCESS;
    }

    public String addAnagrafica(){
        //anagrafica.setRuolo(ruolo);
        //dao.salvaAnagrafica(anagrafica);
        return "success";
    }

    @Override
    public Anagrafica getModel() {
        return anagrafica;
    }

}
jsp中

表单

<s:form action="addAnagrafica">
    <s:textfield name="Nome" label="Nome" />
    <s:textfield name="Cognome" label="Cognome" />
    <s:textfield name="Provincia" label="Provincia" />
    <s:textfield name="Codice_fiscale" label="Codice_fiscale" />
    <s:select label="Ruolo" headerKey="-1"
        headerValue="Seleziona il ruolo" list="ruoli" listKey="id"
        listValue="descrizione" name="Ruolo.id"/>
    <s:submit />
    <hr />
</s:form>
提交插入后

错误

org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved beforeQuery current operation : it.unirc.pjam.Anagrafica.Model.Anagrafica.ruolo -> it.unirc.pjam.Ruolo.Model.Ruolo
    at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:122)
    at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:418)
    at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:621)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:684)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669)
    at it.unirc.pjam.Anagrafica.DAO.AnagraficaDAOHibernateImpl.salvaAnagrafica(AnagraficaDAOHibernateImpl.java:42)
    at it.unirc.pjam.Action.AnagraficaStruts.addAnagrafica(AnagraficaStruts.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:873)
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1539)
    at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:96)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:88)
    at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1615)
    at ognl.ASTMethod.getValueBody(ASTMethod.java:91)
    at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at ognl.SimpleNode.getValue(SimpleNode.java:258)
    at ognl.Ognl.getValue(Ognl.java:467)
    at ognl.Ognl.getValue(Ognl.java:431)
    at com.opensymphony.xwork2.ognl.OgnlUtil$3.execute(OgnlUtil.java:351)
    at com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecuteMethod(OgnlUtil.java:403)
    at com.opensymphony.xwork2.ognl.OgnlUtil.callMethod(OgnlUtil.java:349)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:436)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:253)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:177)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:260)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:73)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.doIntercept(ConversionErrorInterceptor.java:139)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:133)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:133)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:192)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:69)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:115)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:88)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:246)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:99)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:139)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:155)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:174)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at org.apache.struts2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:120)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:171)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:195)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:193)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at it.unirc.pjam.utils.AuthenticationInterceptor.intercept(AuthenticationInterceptor.java:43)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:247)
    at org.apache.struts2.factory.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)
    at org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:81)
    at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:143)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1368)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

看起来有Ruolo类型的实体已分配给您的Anagrafica实例,但Ruolo仍处于暂态(新)状态。这意味着,Ruolo - 它来自哪里 - 在你当前的持久化环境中并不是“已知的”。 Ruolo上的Anagrafica属性似乎也是必需的(非空约束),因此在刷新时你必须得到以下异常:

  

[...] TransientPropertyValueException:非null属性引用瞬态值[...]

要解决此问题,您必须在保存Ruolo之前保存Anagrafica,或者为传递持久性定义策略,a.k.a CascadeType。你必须决定什么适合你的用例。

详细了解Entity states和。{ transitive persistence在文档中或查看Vlad的初学者关于Entity state transitions with JPA and HibernateHibernate Cascade Types的指南。