为什么我使用此代码示例获得org.hibernate.MappingException?

时间:2015-02-20 10:53:00

标签: java hibernate

.hibernate.MappingException: Repeated column in mapping for entity: com.sample.User2 column: CITY_NAME (should be mapped with insert="false" update="false")

是我在运行使用Hibernate和MSSQL Server的程序时遇到的异常。这是我的代码,来自在线教程。我不确定问题出在哪里,我当然搜索了很多,但我发现的一切都有一个更明显的错误。我似乎无法在这里找到它。请记住,这是我使用Hibernate / JPA的第二天。

com.sample.Address.java:

package com.sample;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {

    @Column(name="CITY_STREET")
    private String street;
    @Column(name="CITY_NAME")
    private String city;
    @Column(name="STATE")
    private String state;
    @Column(name="CITY_ZIP")
    private String zip;

    public Address() { }

    public Address(String street, String city, String state, String zip) {
        this.street = street;
        this.city = city;
        this.state = state;
        this.zip = zip;
    }
    //Setters and getters generate by Eclipse (omitted for length)
    //Note: No annotations on methods
}

com.sample.User2:

package com.sample;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="FancyTable")
public class User2 {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="USER_ID")
    private int userId;
    @Column(name="USER_NAME")
    private String userName;
    @Embedded
    @AttributeOverrides({
        @AttributeOverride(column = @Column(name="HOME_STREET_NAME"), name = "CITY_STREET"),
        @AttributeOverride(column = @Column(name="HOME_CITY_NAME"), name = "CITY_NAME"),
        @AttributeOverride(column = @Column(name="HOME_STATE"), name = "STATE"),
        @AttributeOverride(column = @Column(name="HOME_CITY_ZIP"), name = "CITY_ZIP")})
    private Address homeAddress;
    @Embedded
    private Address officeAddress;

    public User2() { }

    //Setters and getters generated by Eclipse (Omitted for length)
    //Note: No annotations on methods
}

com.sample.HibernateTest3.java:

package com.sample;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HibernateTest3 {

    static void run() {

        User2 user = new User2();
        User2 user2 = new User2();

        user.setUserName("Test");
        user2.setUserName("Test 2");

        Address add1 = new Address();
        add1.setStreet("street 1");
        add1.setCity("city 1");
        add1.setState("state 1");
        add1.setZip("zip 1");

        Address add2 = new Address();
        add2.setStreet("street 2");
        add2.setCity("city 2");
        add2.setState("state 2");
        add2.setZip("zip 2");

        user.setHomeAddress(add1);
        user2.setHomeAddress(add2);

        user.setOfficeAddress(new Address("a", "b", "c", "d"));
        user.setOfficeAddress(new Address("X", "X", "X", "X"));


        SessionFactory sf = null;

        try {

            sf = HibernateUtils.createSessionFactory();

            Session session = sf.openSession();

            session.beginTransaction();
            session.save(user);
            session.save(user2);
            session.getTransaction().commit();

        } catch (Exception e) {

            System.out.println("ERROR");

            e.printStackTrace();

        } finally {

            try {

                HibernateUtils.close();

            } catch (Exception ex) {
                System.out.println("ERROR 2");

                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        run();
    }
}

最后,我的hibernate.cfg.xml文件......

<?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property       name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=sample1</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">OMMITED</property>

        <!-- MSSQL Dialect -->
        <property  name="dialect">org.hibernate.dialect.SQLServerDialect</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</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">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.sample.User2"/>

    </session-factory>

</hibernate-configuration>

有人可以说清楚并告诉我究竟是什么导致了这个错误?

1 个答案:

答案 0 :(得分:3)

来自@AttributeOverride

的文档
  

(必需)正在覆盖其映射的属性的名称   如果正在使用基于属性的访问,或者如果使用字段的名称   使用基于字段的访问。

因此您应该使用字段名称而不是列名称,例如

@AttributeOverride(column = @Column(name="HOME_STREET_NAME"), name = "street")

否则列名称不会更改,您将获得例外