当前位置: 首页 > 知识库问答 >
问题:

无法创建唯一键约束

成浩漫
2023-03-14

我正在创建一个简单的实体,并试图将它持久化到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的好资源,因为在线文档对于像我这样的初学者来说不是一个好的资源。

共有3个答案

孔阳炎
2023-03-14

当我尝试在类 A 上的列“a_id”和“b_id”列上创建唯一约束并且我没有用 @ManyToOne 注释 B 时,问题出现在我身上,因此 Hibernate 对列“b_id”一无所知(作为类 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
   }
南宫凯康
2023-03-14

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

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

齐兴运
2023-03-14

我也有同样的错误,通过显式设置@科列(name="field_name")@UniqueConstraint中使用的属性来解决。

 类似资料:
  • 我有一个使用Hibernate访问数据库的应用程序。它会抛出如下错误: 组织Spring框架清洁工厂。BeanCreationException:创建URL[文件:/C:/Program Files(x86)/XXX/applicationContext.xml]中定义的名为“sessionFactory”的bean时出错:初始化方法调用失败;嵌套异常是org.hibernate。Annotati

  • 我在创建mysql数据库中现有表的外键时遇到了一些问题。 我不想创建一个名为的新表来引用它,使用以下方法: 但我发现了错误: 为了获得更多的信息,我做了: 在我看来,这两个列的类型似乎是匹配的,因为它们都是varchar(45)。(我还尝试将列设置为not null,但这没有解决问题)所以我猜问题一定是。但我不太清楚这意味着什么,也不知道如何检查/修复它。有人有什么建议吗?是什么意思?

  • 问题内容: 我有一个具有这种布局的表: 我想创建一个类似于以下的唯一约束: 但是,这将允许多行具有相同的if 。我想允许在存储不具有关联菜单中的最爱,但我只希望每个用户/食谱对这些行中最多只有一个。 到目前为止,我的想法是: 使用一些硬编码的UUID(例如全零)而不是null。 但是,每个用户的菜单都有FK约束,因此我不得不为每个用户创建一个特殊的“空”菜单,这很麻烦。 而是使用触发器检查是否存在

  • 表a已成功创建... 错误1215(HY000):无法添加外键约束 无法创建表b

  • Liquibase文件如下: 数据库更改日志: 我在启动spring boot应用程序时添加唯一约束时遇到以下错误 迁移更改集失败 /db/changelog/changes/1.create-account-balance-table.yml::1::roran:原因:liquibase.exception.数据库异常: ERROR:语法错误在或接近PARTITION位置: 93[失败SQL:(

  • 注意 当前章节中涉及的配置一般适用于关系数据库。这里展示的扩展方法在你安装了关系数据库提供程序之后就能获得(由Microsoft.EntityFrmeworkCore.Relational 程序包共享)。 唯一约束是为模型中的替代键引入的。 惯例 按照惯例,为替代键引入的索引和约束都被命名为 AX_<实体类型名称>_<属性名称>。对于组合替代键,<属性名> 为用下划线分隔的属性名。 数据注解 不能