我在使用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

所以,这些是我的类(class)。但是,当我想运行我的应用程序时,会遇到下一个异常: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")

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

最佳答案

您的match类中的两个变量playerPerformance与嵌入式ID中的matchIdplayerId映射到相同的列。如错误所述,它们“应与insert =“false” update =“false”“映射。

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

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

这实质上使这些字段为只读,因此,如果更改了嵌入式ID中的值,则Hibernate仅知道更改MATCH_IDPLAYER_ID列,而如果matchplayer的值更改,则不会更改。

10-08 07:06