hibernate not-null属性引用null或transient值:

时间:2012-06-26 13:25:24

标签: hibernate jsf-2 entitymanager hibernate-entitymanager

此问题随机出现。该计划已经运作了一个多月,今天它已经崩溃了。在添加新订单时,用户可以从下拉列表中选择公司。然后他可以将新订单保存到数据库中。即使现在重新加载应用程序,它也可以在大多数时间运行。错误代码如下:

javax.persistence.PersistenceException:org.hibernate.PropertyValueException:not-null属性引用null或transient值:mapped.Order.company(odwzorowania.Zlecenie.firma)

使用Hibernate,JSF 2.1,MySql 5.5,javax.persistence。*

这里是代码(名称是从波兰语翻译而来)

public void update(Order order) throws AdException
{
    try
    {
        begin();

            Company fir2 = order.getCompany();

            em.merge(fir2);
            em.merge(order);

        commit();
    }
    catch( HibernateException e )
    {
        rollback();
        throw new AdException("Nie mozna zaktualizować zlecenia",e);
    }
}

订单实体

    import javax.persistence.* //fixed in real code

    @Entity
    @Table(name = "order")


    public class Order implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer idOrder;
    private Company company;
    [...]

    @JoinColumn(name = "company_idCompany", referencedColumnName = "idCompany")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }
    [...]

公司实体

import javax.persistence.*

@Entity
@Table(name = "company")

public class Company implements Serializable {

private static final long serialVersionUID = 1L;

private Integer idCompany;       
private List<Order> orderList;
[...]

@OneToMany(mappedBy = "company", fetch = FetchType.LAZY)
public List<Order> getOrderList() {
    return orderList;
}

public void setOrderList(List<Order>orderList) {
    this.orderList = orderList;
}
[...]

堆栈跟踪(mapped.Order.company与odwzorowania.Zlecenie.firma从波兰语翻译相同)

2012-xx-xx xx:xx:xx com.sun.faces.application.ApplicationImpl newThing
SEVERE: Unable to load class: 
javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: odwzorowania.Zlecenie.firma
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:74)
    at controllery.FirmaController.get(FirmaController.java:84)
    at validatory.FirmyValidator.<init>(FirmyValidator.java:32)
    at sun.reflect.GeneratedConstructorAccessor154.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
    at java.lang.Class.newInstance0(Class.java:372)
    at java.lang.Class.newInstance(Class.java:325)
    at com.sun.faces.application.ApplicationImpl.newThing(ApplicationImpl.java:1699)
    at com.sun.faces.application.ApplicationImpl.createValidator(ApplicationImpl.java:1557)
    at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.createValidator(ValidatorTagHandlerDelegateImpl.java:238)
    at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.applyAttachedObject(ValidatorTagHandlerDelegateImpl.java:132)
    at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.applyNested(ValidatorTagHandlerDelegateImpl.java:205)
    at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.apply(ValidatorTagHandlerDelegateImpl.java:87)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:86)
    at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:152)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:769)
    at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:232)
    at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:188)
    at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:123)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:452)
    at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:148)
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:192)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:636)
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: odwzorowania.Zlecenie.firma
    at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:270)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:128)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
    at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
    at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:65)
    ... 58 more

我找到了一些答案但其中没有一个似乎适合我的问题。正如您所看到的,我正在更新公司和订单之间双向关联的两个方面。

编辑:修正翻译

1 个答案:

答案 0 :(得分:0)

我认为实体公司与实体订单之间的关系是导致问题的原因。实体订单没有公司。

问题,order.getCompany()在测试期间可能为null?您对实体订单的映射是:

@ManyToOne(**optional = false**, fetch = FetchType.LAZY)

我发现实体订单中需要字段公司。

此致