开发者

Hibernate: @EmbeddedId, Inheritance and @SecondaryTable

开发者 https://www.devze.com 2023-01-24 19:39 出处:网络
I\'m using Hibernate version 3.3.2.GA with annotations. I have inheritance between two classes, the former:

I'm using Hibernate version 3.3.2.GA with annotations.

I have inheritance between two classes, the former:

@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 { }

The subclass is mapped with a secondary table:

@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() {
        retur开发者_如何学Cn getField;
    }
}

Where the field is composed of two different fields

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

Now when I create a query on SubClass the FIELD_1 and FIELD_2 fields are searched on the SuperClass, even if they're defined in the subclass.

I can't set the table in the @Column annotation in the field, because the Field class it's reused somewhere. I need to specify it in SubClass class.

How do I specify that the field should be searched in the secondary table?

Also on Hibernate Forum


You should use table attribute

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

UPDATE

When a embeddable column is used by more than one entity, you should use @AttributeOverride if you need to re-map just a single column or @AttributeOverrides if more than one column

@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; }

    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消