嵌入式id和“实体映射中的重复列...”异常

时间:2015-04-29 19:04:07

标签: java spring hibernate jpa

我遇到了JPA和Hibernate的问题,我无法解决它。

所以,这是我的applicationContext.xml:

<context:component-scan base-package="com.abt.fiifootballmanager">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<jdbc:embedded-database type="HSQL" id="dataSource" />

<bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="emf">
    <property name="packagesToScan" value="com.abt.fiifootballmanager.entity" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
    <property name="persistenceProvider">
        <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="dataSource" value="dataSource"/>
</bean>

这是我的表演实体:

 package com.abt.fiifootballmanager.entity;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;

@Entity
@Table(name="PERFORMANCES")
@NamedQuery(name="Performance.findAll", query="SELECT p FROM Performance p")
public class Performance implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private PerformancePK id;

    @Column(name="RED_CARD")
    private BigDecimal redCard;

    @Column(name="YELLOW_CARD")
    private BigDecimal yellowCard;

    @OneToOne(mappedBy="performance")
    private GoalkeepingPerformance goalkeepingPerformance;

    @OneToMany(mappedBy="performance")
    private List<OutfieldPerformance> outfieldPerformances;

    @ManyToOne
    @JoinColumn(name="MATCH_ID")
    private Match match;

    @ManyToOne
    @JoinColumn(name="PLAYER_ID")
    private Player player;
    ...getters & setters }

这是我的嵌入式id类:

@Embeddable
public class PerformancePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="MATCH_ID", insertable=false, updatable=false)
    private long matchId;

    @Column(name="PLAYER_ID", insertable=false, updatable=false)
    private long playerId;
    ... //getters and setters

所以,这些是我的课程。但是当我想运行我的应用程序时,我得到了下一个例外: 1.Error creating bean with name 'emf' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

2. Repeated column in mapping for entity: com.abt.fiifootballmanager.entity.Performance column: MATCH_ID (should be mapped with insert="false" update="false")

我认为它是由第二个引起的第一个例外。那么,为什么我得到“实体映射中的重复列?”。使用嵌入式id类是个好主意吗?

1 个答案:

答案 0 :(得分:-1)

我遇到了同样的问题,并通过@Embeddedable更改@EmbeddedId / @IdClass解决了这个问题。

你的课程应该是这样的(我省略了一些信息):

@Entity
@Table(name="PERFORMANCES")
@IdClass(PerformancePK.class)
public class Performance implements Serializable {

  @Id
  @Column(name="MATCH_ID")
  private long matchId;

  @Id
  @Column(name="PLAYER_ID")
  private long playerId;

  @ManyToOne
  @JoinColumn(name="MATCH_ID", insertable = false, updatable = false)
  private Match match;

  @ManyToOne
  @JoinColumn(name="PLAYER_ID", insertable = false, updatable = false)
  private Player player;

  ...//getters, setters
}


public class PerformancePK implements Serializable {

    private long matchId;

    private long playerId;

    ... //getters and setters

    // !!! MANDATORY: override hashCode() and equals() here
}

通过这种结构,您的数据库结构仍然相同。 有关@IdClass usage的更多信息。