简体   繁体   中英

Unable to create unique key constraint

I am creating a simple entity and trying to persist it to Oracle database. Here is my enitity:

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;
    }
}

Here is my Java class that creates an instance of this entity and saves it to database using 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();
    }
}

When I run this program then I am getting an exception as :

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)

Please help where I am making mistake in this code. I am using Hibernate-4.3.6

Update: Here is my hibernate configuration file, the table gets generated by hibernate itself:

<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>

Also if possible please suggest me a good resource for Hibernate-4.3, because the online document is not a good resource for starters like me.

You are using field access strategy (determined by @Id annotation). Put any JPA related annotation right above each field instead of getter property

As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the @Id annotation gives the default access strategy. When placed on a field, Hibernate will assume field-based access. Placed on the identifier getter, Hibernate will use property-based access

The problem appears with me when i was trying to create a unique constraint on columns "a_id" and "b_id" on Class A and i was not annotating B with @ManyToOne so Hibernate doesn't know anything about the column "b_id" (as foreign key of class B) :

   @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
   }

The full error text is:

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)

You have most likely misspelled one of the column names.

我遇到了同样的错误,它通过将@Column(name = "field_name")显式设置为@UniqueConstraint使用的属性来@UniqueConstraint

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM