org.hibernate.MappingException:无法在Spring Hibernate

时间:2016-05-18 09:41:11

标签: java spring hibernate spring-mvc

我正在将Hibernate项目转换为Spring Hibernate项目。由于我正在将Hibernate与Spring集成,因此我在@Configuration中定义了所有会话工厂,数据源定义。部署之后,当我启动服务器时,我正处于异常

之下
Unable to find column with logical name: TEST_CODE in org.hibernate.mapping.Table(TEST_MONTHS) and its related supertables and secondary tables.

EntityClass:

@javax.persistence.Entity
@Table(name="TEST_MONTHS")

public class ConnectionMonth implements Serializable {

    /**
     * @generated
     */
    private static final long serialVersionUID = -940496855L;

    @Id
    @Column(name = "TEST_CODE")
    private String testCode;

    @Id
    @Column(name = "CONMONTH")
    private String contractMonth;

    @Column(name = "TYPE")
    private String type;

    @Column(name = "STARTDATE")
    private Date startDate;

    @Column(name = "ENDDATE")
    private Date endDate;


    public String gettestCode() {
        return testCode;
    }

    public void settestCode(String testCode) {
        this.testCode = testCode;
    }

    public String getContractMonth() {
        return contractMonth;
    }

    public void setContractMonth(String contractMonth) {
        this.contractMonth = contractMonth;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    /**
     * @generated
     */
    @Override
    public String toString() {

        return "ContractMonth: " + contractMonth + " Type: " + type
                + " StartDate: " + startDate + " EndDate:  " + endDate;

    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((testCode == null) ? 0 : testCode.hashCode());
        result = prime * result
                + ((contractMonth == null) ? 0 : contractMonth.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ContractMonth other = (ContractMonth) obj;
        if (testCode == null) {
            if (other.testCode != null)
                return false;
        } else if (!testCode.equals(other.testCode))
            return false;
        if (contractMonth == null) {
            if (other.contractMonth != null)
                return false;
        } else if (!contractMonth.equals(other.contractMonth))
            return false;
        return true;
    }

}

持久性配置:

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence.properties" })
@ComponentScan({ "com.test.commons.domain.*" })
public class PersistenceConfiguration {

    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(restDataSource());
        sessionFactory.setPackagesToScan(new String[] {
                "com.test.commons.domain.employee"});
        sessionFactory.setHibernateProperties(hibernateProperties());

        return sessionFactory;
    }

    @Bean
    public DataSource restDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.user"));
        dataSource.setPassword(env.getProperty("jdbc.pass"));

        return dataSource;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(
            SessionFactory sessionFactory) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(sessionFactory);

        return txManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    Properties hibernateProperties() {
        return new Properties() {
            {
                setProperty("hibernate.hbm2ddl.auto",
                        env.getProperty("hibernate.hbm2ddl.auto"));
                setProperty("hibernate.dialect",
                        env.getProperty("hibernate.dialect"));
                setProperty("hibernate.globally_quoted_identifiers", "true");
            }
        };
    }
}

persistence.properties:

# jdbc.X
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=/*DB url*/
jdbc.user=pwd
jdbc.pass=pwd

# hibernate.X
hibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop

1 个答案:

答案 0 :(得分:0)

你能检查一下sql中的表格详细信息,它是否有“”引号或单引号

因为我在我的代码中也面临同样的问题,因为我的表格详情如下

CREATE TABLE `orders` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`firstname` VARCHAR(50) NOT NULL,
);

我正在使用下面的bean访问它

@Entity
@Table(name="orders")
public class Order {

    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;

    @Column(name="firstname")
    private String firstName;

    @OneToMany(targetEntity=OrderLine.class,
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY)
    @JoinTable(
            name="orderlines",
            joinColumns = {@JoinColumn(table = "orderlines", name="FK_orders_orders", 
            referencedColumnName = "order_id")},
            inverseJoinColumns={@JoinColumn(table = "orders", name="FK_orders_orders", 
            referencedColumnName = "id")}
            ) 

删除单引号后,它开始为我工作