Hibernate:@ EmbeddedId,Inheritance和@SecondaryTable

时间:2010-11-11 14:27:45

标签: java hibernate orm mapping

我正在使用带有注释的Hibernate版本3.3.2.GA。

我有两个类之间的继承,前者:

@Entity
@Table(name = "SUPER_CLASS")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="DISCR_TYPE",
    discriminatorType= DiscriminatorType.STRING
)
@org.hibernate.annotations.Entity(mutable = false)
public class SuperClass { }

子类与辅助表映射:

@Entity
@DiscriminatorValue("VALUE")
@org.hibernate.annotations.Entity(mutable = false)
@SecondaryTable(name = "V_SECONDARY_TABLE",
        pkJoinColumns = @PrimaryKeyJoinColumn(name = "ID", referencedColumnName = "ID"))
public class SubClass extends SuperClass  { 
 @Embedded
    public Field getField() {
        return getField;
    }
}

该字段由两个不同的字段组成

@Embeddable
public class Field { 
 @Column("FIELD_1") String field1
 @Column("FIELD_2") String field2
}

现在,当我在SubClass上创建查询时,在SuperClass上搜索FIELD_1和FIELD_2字段,即使它们是在子类中定义的。

我无法在字段中的@Column注释中设置表,因为它在某处重用了Field类。我需要在SubClass类中指定它。

如何指定应在辅助表中搜索字段?

同样在Hibernate Forum

1 个答案:

答案 0 :(得分:5)

您应该使用 table 属性

@Column("FIELD_1", table="V_SECONDARY_TABLE")

<强>更新

当多个实体使用可嵌入列时,如果需要重新映射单个列,则应使用@AttributeOverride;如果需要多列,则应使用@AttributeOverrides

@Entity
@SecondaryTable(name="OTHER_PERSON")
@AttributeOverride(name="address.street", column=@Column(name="STREET", table="OTHER_PERSON"))
public class Person {

    private Address address;

    @Id
    @GeneratedValue
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }

    @Embedded
    public Address getAddress() { return address; }
    public void setAddress(Address address) { this.address = address; }

    @Embeddable
    public static class Address implements Serializable {

        private String address;

        public String getStreet() { return street; }
        public void setStreet(String street) { this.street = street; }

    }

}