1

我正在尝试与 hibernate4 创建多对多关联。我正在做的是:

    @javax.persistence.Table(name = "devices", schema = "", catalog = "explorer_user")
@Entity
public class DevicesEntity {


    private int id;

    @javax.persistence.Column(name = "id", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    private Collection<PositionsEntity> positions;//= new HashSet<PositionsEntity>();

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "positions_has_devices",     catalog = "explorer_user" ,
            joinColumns = { @JoinColumn(name = "devices_id") },
            inverseJoinColumns = { @JoinColumn(name = "positions_id") })
    public Collection<PositionsEntity> getPositions(){
        return this.positions;
    }

    private void setPositions(Collection<PositionsEntity> positions){
        this.positions = positions;
    }
}

@javax.persistence.Table(name = "positions", schema = "", catalog = "explorer_user")
    @Entity
public class PositionsEntity {
    private int id;

    @javax.persistence.Column(name = "id", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    private Collection<DevicesEntity> devices;// = new HashSet<DevicesEntity>();

    @ManyToMany(mappedBy = "positions")
    public Collection<DevicesEntity> getDevices() {
        return this.devices;
    }

    public void setDevices(Collection<DevicesEntity> d) {
        this.devices = d;
    }
}



@javax.persistence.IdClass(com.printerauditor.jpa.PositionsHasDevicesEntityPK.class)
@javax.persistence.Table(name = "positions_has_devices", schema = "", catalog = "explorer_user")
@Entity
public class PositionsHasDevicesEntity {
    private int positionsId;

    @javax.persistence.Column(name = "positions_id", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    public int getPositionsId() {
        return positionsId;
    }

    public void setPositionsId(int positionsId) {
        this.positionsId = positionsId;
    }

    private int devicesId;

    @javax.persistence.Column(name = "devices_id", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    public int getDevicesId() {
        return devicesId;
    }

    public void setDevicesId(int devicesId) {
        this.devicesId = devicesId;
    }

    private int devicesPrinterLicenceId;

    @javax.persistence.Column(name = "devices_printer_licence_id", nullable = false,     insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    public int getDevicesPrinterLicenceId() {
        return devicesPrinterLicenceId;
    }

    public void setDevicesPrinterLicenceId(int devicesPrinterLicenceId) {
        this.devicesPrinterLicenceId = devicesPrinterLicenceId;
    }
}

上面显示的所有表格都存在,许多注释基于 mkyong 的教程。在部署应用程序期间抛出异常。亲爱的根本原因:

org.hibernate.MappingException: Unable to find physical table: `devices`
org.hibernate.cfg.Configuration$MappingsImpl.getLogicalTableName(Configuration.java:2881)
org.hibernate.cfg.Configuration$MappingsImpl.getLogicalTableName(Configuration.java:2874)
org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1132)
org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:733)
org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:668)
org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:69)
org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1611)
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1369)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1756)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)
org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:372)
org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:357)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:420)
org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:277)
org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:139)
org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:79)
org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:70)
org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:85)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1506)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1474)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:741)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2430)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2419)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

Hibernate.cfg 包含这些表的正确映射,intelliJ 识别这些关联。你有什么想法,会出什么问题?

4

3 回答 3

0

奇怪的事情,但是将 JPA 模块转换为 Maven 项目解决了这个问题。

于 2013-11-15T07:44:31.827 回答
0

尝试'schema = "", catalog = "'@javax.persistence.Table注释类 DevicesEntity中删除

于 2015-06-23T22:13:24.867 回答
0

schema="myDb"奇怪的是,在我的情况下,从@Table注释中删除就足够了(@Entity在类语句之后和之前)。Hibernate 在这里做了一些乱七八糟的事情,尽管没有模式定义,但表是在正确的数据库中创建的。

这个例子工作正常:

@Entity
@Table(name="foo") //works
public class Foo {
...
@ManyToMany ... //...and here follows all other stuff...
...
}

这个例子不起作用:

@Entity
@Table(name="foo", schema="dbBar") //doesn't work
public class Foo {
...
@ManyToMany ... // ...and here follows all other stuff...
...
}
于 2016-05-19T15:32:43.127 回答