我正在使用带有注释的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
}
现在,当我在子类上创建查询时,即使在子类中定义了FIELD_1和FIELD_2字段,它们也会在SuperClass上进行搜索.
我无法在字段的@Column批注中设置表格,因为它在某些地方重复使用了Field类.我需要在SubClass类中指定它.
如何指定应该在辅助表中搜索的字段?
解决方法:
您应该使用表格属性
@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; }
}
}