无法创建唯一键约束

时间:2014-08-09 18:52:45

标签: java hibernate

我正在创建一个简单的实体并尝试将其持久化到Oracle数据库。这是我的慷慨:

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "TBL_FLIGHT", uniqueConstraints = @UniqueConstraint(name = "flight_number", columnNames = {
        "comp_prefix", "flight_number" }))
public class Flight implements Serializable {
    @Id 
    private Long id;
    private String companyPrefix;
    private String number;

    public Long getId() {
        return id;
    }

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

    public void setCompanyPrefix(String companyPrefix) {
        this.companyPrefix = companyPrefix;
    }

    public void setNumber(String number) {
        this.number = number;
    }   

    @Column(name = "comp_prefix")
    public String getCompanyPrefix() {
        return companyPrefix;
    }

    @Column(name = "flight_number")
    public String getNumber() {
        return number;
    }
}

这是我的Java类,它创建了这个实体的实例,并使用Hibernate将其保存到数据库:

public class AppTest{

    public static void main(String[] args) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Flight flight = new Flight();
        flight.setCompanyPrefix("prefix");;
        flight.setNumber("100");
        flight.setId(1L);
        session.save(flight);

        session.getTransaction().commit();          

        HibernateUtil.getSessionFactory().close();
    }
}

当我运行这个程序时,我得到一个例外:

Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (comp_prefix, flight_number) on table TBL_FLIGHT: database column 'comp_prefix', 'flight_number' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
    at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1682)
    at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1614)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1450)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)

请帮助我在此代码中犯错的地方。我正在使用Hibernate-4.3.6

更新:这是我的hibernate配置文件,该表由hibernate本身生成:

<session-factory>

    <!-- Database connection settings -->
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
    <property name="hibernate.connection.username">myuser</property>
    <property name="hibernate.connection.password">mypasswd</property>
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>

    <mapping class="org.hibernate.tutorial.Flight" />
</session-factory>

如果可能的话,请为Hibernate-4.3建议一个很好的资源,因为online document不是像我这样的初学者的好资源。

4 个答案:

答案 0 :(得分:1)

您正在使用字段访问策略(由@Id注释确定)。将任何与JPA相关的注释放在每个字段的正上方而不是getter属性

  

作为JPA提供者,Hibernate可以自省两个实体属性   (实例字段)或访问者(实例属性)。默认情况下,   @Id注释的位置给出了默认的访问策略。   放置在字段上时,Hibernate将采用基于字段的访问。   放置在标识符getter上,Hibernate将使用基于属性的   访问

答案 1 :(得分:1)

当我不使用@ManyToOne注释B时,问题就出现了,所以Hibernate不知道A中的“ b_id”列:

   @Entity
   @Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "a_id", "b_id" }) })
   Class A {
      @Id
      a_id;

      // @ManyToOne was not annotated
      B b;
   }

   @Entity
   Class B {
      @Id
      b_id
   }

答案 2 :(得分:0)

完整的错误文字是:

  

引起:org.hibernate.AnnotationException:无法在表TBL_FLIGHT上创建唯一键约束(comp_prefix,flight_number):数据库列&#39; comp_prefix&#39;,&#39; flight_number&#39;未找到。确保使用正确的列名,这取决于使用的命名策略(它可能与实体中的属性名称不同,特别是对于关系类型)

你很可能拼错了其中一个列名。

答案 3 :(得分:0)

我遇到了同样的错误,通过显式设置 @Column(name = "field_name") 解决了 @UniqueConstraint 中使用的属性。