java - Hibernate/JPA 映射同名的复合主键和外键

标签 java hibernate jpa

我的数据库是:

TABLE1
"FILE_ID" NUMBER NOT NULL ENABLE, 
"GEO_ZONE" VARCHAR2(2 BYTE) NOT NULL ENABLE,
PRIMARY KEY ("FILE_ID", "GEO_ZONE")

TABLE2
"FILE_ID" NUMBER NOT NULL ENABLE, 
"GEO_ZONE" VARCHAR2(2 BYTE) NOT NULL ENABLE,
"RECORD_NUM" NUMBER, 
PRIMARY KEY ("FILE_ID", "GEO_ZONE", "RECORD_NUM"),
CONSTRAINT "FK_8ULB8IEBEU6A0VK1WTNQA3MCY" FOREIGN KEY ("FILE_ID", "GEO_ZONE")

表 1 中的 1 行可以在表 2 中包含多行。

我的 TABLE1 实体是:

@Entity
@Table(name = "TABLE1")
@IdClass(Table1Id.class)
public class Table1Entity implements Serializable {
private static final long serialVersionUID = 7825109721507305471L;

@Id
@Column(name = "FILE_ID", insertable = false, updatable = false)
private Long fileId;

@Id
@Column(name = "GEO_ZONE", insertable = false, updatable = false)
private String geoZone;

... Others attributes and getter and setter

我的 Table1Id 类是:

public class Table1Id implements Serializable {
    private static final long serialVersionUID = -8618317422024959144L;

    private Long fileId;

    private String geoZone;

... Others attributes and getter and setter

我的 TABLE2 实体是:

@Entity
@Table(name = "TABLE2")
@IdClass(Table2Id.class)
public class Table2Entity implements Serializable {

    private static final long serialVersionUID = -1344497166638156145L;

    @Id
    @Column(name = "FILE_ID", insertable = false, updatable = false)
    private Long fileId;

    @Id
    @Column(name = "GEO_ZONE", insertable = false, updatable = false)
    private String geoZone;

    @Id
    @Column(name = "RECORD_NUM")
    private Long recordNum;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumns({@JoinColumn(name = "FILE_ID", insertable = false, updatable = false), //
            @JoinColumn(name = "GEO_ZONE", insertable = false, updatable = false)})
    private Table1Entity table1Entity;


... Others attributes and getter and setter

我的 Table2Id 类是:

public class Table2Id implements Serializable {

    private static final long serialVersionUID = -4599660767213338871L;

    private Long fileId;

    private String geoZone;

    private Long recordNum;

... Others attributes and getter and setter

当我尝试启动 Tomcat 时,出现以下错误:

org.hibernate.MappingException: Foreign key (FK_8ulb8iebeu6a0vk1wtnqa3mcy:TABLE2 [FILE_ID,GEO_ZONE])) must have same number of columns as the referenced primary key (TABLE1 [FILE_ID])

我尝试使用引用列、主键连接列以及许多其他东西,但通过在互联网上阅读它,它可以解决数据库建模问题。 我认为问题是主键和外键在两个表中具有相同的名称,但我可能是错的...... 请您确认一下或者您是否有解决方案。

提前感谢您,因为我花了 1 周的时间寻找解决方案,但没有找到有效的解决方案。

编辑: 我将表名称更改为 toto,它在我的库中不存在,并且我与另一个 fk id 出现相同的错误。 hibernate 似乎无法连接到我的数据库。 但是,如果我从 TABLE1 中删除复合键,以便在 Table2Entity 的 join_column 中仅包含 file_id,并且项目可以工作,但这不是我想要的。 这让我很恶心,我根本不明白问题出在哪里。 错误告诉 RDJ_STAT pk 只是 FILE_ID 但不是,就像 hibernate 喝醉了一样

编辑2: ojdbc 版本可能有问题吗? 没有解决办法吗?

编辑3:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa">

    <context:property-placeholder location="classpath:application.properties" />

    <context:annotation-config/>

     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${db.driver}" />
            <property name="url" value="${db.url}" />
            <property name="username" value="${db.username}" />
            <property name="password" value="${db.password}" />
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true"/>
        <property name="generateDdl" value="true"/>
        <property name="database" value="MYSQL"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="persistenceXmlLocation" value="classpath:persistence.xml"></property>
        <!-- spring based scanning for entity classes>-->
    </bean>

     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>

    <jpa:repositories base-package="com.sacre.repository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>

</beans>

最佳答案

错误的问题是 @ManyToOne 尝试使用 @OneToOne:

    @OneToMany(fetch = FetchType.EAGER)
    ...
    private Table1Entity table1Entity;

关于java - Hibernate/JPA 映射同名的复合主键和外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35575718/

相关文章:

Java fx 缓慢移动的图像

java - 使用查询更新对象外键

Java Spring + Hibernate LocalSessionFactoryBean 不工作

java - JPA删除双向实体

java - 使用枚举作为 id

java - 在字符串问题中存储 XPath 查询 xml

java - 非唯一列上的 HIbernate onetoMany 映射

java - Dagger 2 : Injecting member which has constructor parameters

java - MySQLNonTransientConnectionException : Too many connections

java - 通过嵌套列表对象中的多个值查找对象 Hibernate、Jpa