造成MappingException的次数很多

时间:2013-11-13 15:32:20

标签: hibernate nhibernate-mapping mappingexception

我试图用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识别这些表。你有什么想法,可能会出错吗?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

尝试删除'schema = "", catalog = "'注释类DevicesEntity

中的@javax.persistence.Table

答案 2 :(得分: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...
...
}