关系非主键@OneToOne Hibernate JPA

时间:2016-08-25 17:15:59

标签: java hibernate jpa

我尝试与我的表Loj_Cupons建立非主键关系到Fat_Pedido,并从SQL Server返回一个选择值。

我从休眠中发现了这个错误。

Caused by: org.hibernate.MappingException: broken column mapping for: lojCupom.id of: br.com.microdatasistemas.simintegrationws.entity.FatPedido

我尝试了很多次并改变了关系,Fat_Pedido也变成了Loj_Cupons。

ON Loj_Cupons表。

@OneToOne(fetch = FetchType.EAGER)
            @JoinColumns({
                    @JoinColumn(name = "Empresa_Pedido", referencedColumnName = "Empresa", insertable = false, updatable = false),
                    @JoinColumn(name = "Pedido_Fat", referencedColumnName = "Pedido", insertable = false, updatable = false) })
            private FatPedido fatPedido;

ON Fat_Pedido表。

@OneToOne(mappedBy = "lojCupom")
    private LojCupom lojCupom;

任何人都知道如何建立正确的关系?我用Google搜索了2天,但没有找到真正的答案。

我的数据库列中的图片。 enter image description here

来自Java的课程。

@Entity
@Table(name = "Loj_Cupons")
public class LojCupom implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private LojCupomPK id;

    @Column(name = "Empresa_Pedido")
    private String empresaPedido;

    @Column(name = "Pedido_Fat")
    private String pedidoFat;

    @OneToOne(mappedBy = "lojCupom")     
    private FatPedido fatPedido;

    public LojCupom() {
        this.setId(new LojCupomPK());
    }

    getters/setters!    
}



@Embeddable
public class LojCupomPK implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "Empresa",insertable=false,updatable=false)
    private String empresa;

    @Column(name = "Maquina",insertable=false,updatable=false)
    private String maquina; 

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "Data",insertable=false,updatable=false)
    private Calendar data;  

    @Column(name = "Controle",insertable=false,updatable=false)
    private Long controle;  

    public LojCupomPK() {}

    public LojCupomPK(String Empresa, String Maquina, Calendar Data, Long Controle) {
        this.setEmpresa(Empresa);
        this.setMaquina(Maquina);
        this.setData(Data);
        this.setControle(Controle);     
    }

    getters/setters!    

    HashCode() & Equals()
}



@Entity
@Table(name = "Fat_Pedido")
public class FatPedido implements Serializable {
    private static final long serialVersionUID = -1618058067896057649L;

    @EmbeddedId
    private FatPedidoPK id;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumns({
            @JoinColumn(name = "Empresa", referencedColumnName = "Empresa_Pedido", insertable = false, updatable = false),
            @JoinColumn(name = "Pedido", referencedColumnName = "Pedido_Fat", insertable = false, updatable = false) })
    private LojCupom lojCupom;

    public FatPedido() {
        this.setId(new FatPedidoPK());
    }

    getters/setters! for id.



@Embeddable
public class FatPedidoPK implements Serializable {
    private static final long serialVersionUID = 249206612929570749L;

    @Column(name = "Empresa",insertable = false, updatable = false)
    private String empresa;


    @Column(name = "Pedido",insertable = false, updatable = false)
    private String pedido;

    public FatPedidoPK() {
    }

    public FatPedidoPK(String empresa,String pedido ){
        this.setEmpresa(empresa);
        this.setPedido(pedido);
    }

    getters/setters!

    HashCode() & Equals()

}

调用工厂的类。

public LojCupom findLojCupons(LojCupom lojCupom) throws Exception {
        return new LojCupomFactory().findLojCupons(lojCupom);
}


public LojCuponsOM convertFrom(LojCupom lojCupom, LojCuponsOM lojCuponsOM) {

         lojCuponsOM.setEmpresaPedido(lojCupom.getFatPedido().getId().getEmpresa());
         lojCuponsOM.setPedidoFat(lojCupom.getFatPedido().getId().getPedido());
         lojCuponsOM.getFatPedido().setNrNota(lojCupom.getFatPedido().getNrNota());
         lojCuponsOM.getFatPedido().setSerie(lojCupom.getFatPedido().getSerie());
         lojCuponsOM.getFatPedido().setDataNota(lojCupom.getFatPedido().getDataNota());
         lojCuponsOM.getFatPedido().setVrNota(lojCupom.getFatPedido().getVrNota());
}




public class LojCupomFactory {  
    public LojCupom findLojCupons(LojCupom cupons) throws Exception{
        return new LojCupomDAOImple().findLojCupons(cupons);        
    }

}





public class LojCupomDAOImple implements LojCupomDAO {

    @Override
    public LojCupom findLojCupons(LojCupom cupons) throws Exception {
        EntityManager em = FactoryHibernate.getInstance().getEntityManager();

            Criteria criteria = ((Session) em.getDelegate()).createCriteria(LojCupom.class);
            if (cupons != null && cupons.getId().getEmpresa() != null && cupons.getId().getMaquina() != null
                    && cupons.getId().getData() != null && cupons.getId().getControle() != null) {
                criteria.add(Restrictions.eq("id", cupons.getId()));
            }

            cupons = (LojCupom) criteria.uniqueResult();


        return cupons;
}

运行代码时控制台出错。

javax.persistence.PersistenceException: [PersistenceUnit: persistence] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at br.com.microdata.component.persistence.FactoryHibernate.<init>(FactoryHibernate.java:33)
    at br.com.microdata.component.persistence.FactoryHibernate.getInstance(FactoryHibernate.java:39)
    at br.com.microdatasistemas.simintegrationws.dao.impl.LojCupomDAOImple.findLojCupons(LojCupomDAOImple.java:17)
    at br.com.microdatasistemas.simintegrationws.factory.LojCupomFactory.findLojCupons(LojCupomFactory.java:8)
    at br.com.microdatasistemas.simintegrationws.facade.LojCuponsFacade.findLojCupons(LojCuponsFacade.java:43)
    at br.com.microdatasistemas.simintegrationws.facade.LojCuponsFacade.searchLojCupons(LojCuponsFacade.java:34)
    at br.com.microdatasistemas.simintegrationws.impl.LojCuponsImpl.SetLojCupons(LojCuponsImpl.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:212)
    at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
    at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
    at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
    at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
    at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.MappingException: broken column mapping for: lojCupom.id of: br.com.microdatasistemas.simintegrationws.entity.FatPedido
    at org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:180)
    at org.hibernate.persister.entity.AbstractPropertyMapping.initIdentifierPropertyPaths(AbstractPropertyMapping.java:251)
    at org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:224)
    at org.hibernate.persister.entity.AbstractEntityPersister.initOrdinaryPropertyPaths(AbstractEntityPersister.java:1863)
    at org.hibernate.persister.entity.AbstractEntityPersister.initPropertyPaths(AbstractEntityPersister.java:1902)
    at org.hibernate.persister.entity.AbstractEntityPersister.postConstruct(AbstractEntityPersister.java:3128)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:439)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)
    ... 43 more

1 个答案:

答案 0 :(得分:0)

查看数据库表我看到表Loj_Cupons包含外键。因此,与此表对应的实体,即LojCupom应该是OneToOne关系的所有者。这是您必须放置JoinColumns注释的实体:(注意更改的连接列名称:Empresa_Pedido(FK) - &gt; Empresa(PK)和Pedido_Fat(FK) - &gt; Pedido(PK) )

@Entity
@Table(name = "Loj_Cupons")
public class LojCupom implements Serializable {#

    ...

    @OneToOne     
    @JoinColumns({
        @JoinColumn(name = "Empresa_Pedido", referencedColumnName = "Empresa"),
        @JoinColumn(name = "Pedido_Fat", referencedColumnName = "Pedido") })
    private FatPedido fatPedido;

    ...

}

实体FatPedido是反面的;所以你按如下方式注释:

@Entity
@Table(name = "Fat_Pedido")
public class FatPedido implements Serializable {

    ...

    @OneToOne(mappedBy = "fatPedido")
    private LojCupom lojCupom;

    ...

}

注意:我删除了insertable = false属性只是为了节省一些按键;并且updateable = false不是必需的,因为ID字段不可修改。如果您认为您的实体只读,则可以使用它们。

希望它对你有所帮助。