简体   繁体   中英

Inheritance annotation - Exception in thread “main” org.hibernate.MappingException: Repeated column in mapping for entity

I have two class as below. The "role" field is "duplicated".

@Entity(name="INHERITANCE_S1_EMPLOYEE_ANN")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="DISCRIMINATOR", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="EMPLOYEE")
public class Employee {
    @Id
    @Column(name="EMPLOYEE_ID")
    private int id = 0;
    private String name = null;
    private String role = null;

    public Employee(String name){
        setName(name);
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

@Entity
@DiscriminatorValue(value="EXECUTIVE")
public class Executive extends Employee{
    private String role = null;

    public Executive(String name){
        super(name);
    }
    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

My XML is as below.

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.cache.provider_class">org.hibernate.cache.infinispan.InfinispanRegionFactory</property>
        <property name="connection.url">jdbc:derby://localhost:1527/JH</property>
        <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="connection.username">app</property>
        <property name="connection.password">app</property>
        <property name="connection.pool_size">0</property>
        <property name="dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
    </session-factory>
</hibernate-configuration>

How to correct this situation? Thanks a lot.

Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity: com.madhusudhan.jh.advanced.inheritance.s1.Executive column: role (should be mapped with insert="false" update="false")

I do not want to delete the "role" filed in the class Executive, because they work well under XML mapping as below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.madhusudhan.jh.advanced.inheritance.s1">
  <class name="Employee" table="INHERITANCE_S1_EMPLOYEE" discriminator-value="EMPLOYEE">
    <id  name="id" column="EMPLOYEE_ID">
      <generator class="native"/>
    </id>
    <discriminator column="DISCRIMINATOR" type="string"/>

    <property name="name" column="NAME" />
    <subclass name="Executive" extends="Employee" discriminator-value="EXECUTIVE">
        <property name="role" column="ROLE"/>
    </subclass>

  </class>
</hibernate-mapping>

Executive is extending Employee . So there's no need to redeclare role there. If you remove it from Executive , it would still work. It would just simply be inheriting the properties. Are you trying to connect them as two relational tables? eg role as foreign key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM