无法创建唯一的密钥约束 [英] Unable to create unique key constraint

查看:87
本文介绍了无法创建唯一的密钥约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的实体并试图将其保存到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;
私人字符串编号;

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;




$ b $ p
$ b

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

  public class AppTest {

public static void main String [] args){

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

航班航班=新航班();
flight.setCompanyPrefix(prefix);;
flight.setNumber(100);
flight.setId(1L);
session.save(flight);

session.getTransaction()。commit();

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

当我运行这个程序时, as:

 引起:org.hibernate.AnnotationException:无法在表TBL_FLIGHT上创建唯一键约束(comp_prefix,flight_number):数据库列'comp_prefix','flight_number'找不到。确保使用正确的列名,这取决于所使用的命名策略(它可能与实体中的属性名称不同,特别是关系类型)$ or $ $ b $ 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本身:




 <会话工厂> 

<! - 数据库连接设置 - >
< 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连接池(使用内置) - >
< property name =connection.pool_size> 1< / property>

<! - 启用Hibernate的自动会话上下文管理 - >
< property name =current_session_context_class>线程< / property>

<! - 禁用二级缓存 - >
< property name =cache.provider_class> org.hibernate.cache.internal.NoCacheProvider< / property>

<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>

<! - 在启动时删除并重新创建数据库模式 - >
< property name =hbm2ddl.auto>更新< / property>

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



一个很好的Hibernate-4.3资源,因为在线文档对于像我这样的初学者来说并不是一个好的资源。

你正在使用字段访问策略(由@ Id注释)。作为JPA提供者,Hibernate可以反省实体属性$ b $(

lock) b(实例字段)或访问器(实例属性)。默认情况下,
@Id注释的位置给出了默认的访问策略。
当放置在一个字段上时,Hibernate将承担基于字段的访问。
放置在标识符getter中,Hibernate将使用基于属性的
访问


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

这篇关于无法创建唯一的密钥约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆