Hibernate: @EmbeddedId, Inheritance, and @SecondaryTable

I am using Hibernate version 3.3.2.GA with annotations.

I have inheritance between two classes, the first one:

@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 maps to an additional 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() {
        return getField;
    }
}

If the field consists of two different fields

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

Now, when I create a request for SubClass, the fields FIELD_1 and FIELD_2 are looked up on SuperClass, even if they are defined in a subclass.

I cannot set the table in the @Column annotation to a field, because the Field class that he reused somewhere. I need to specify it in the SubClass class.

How to indicate that a field should be searched in the secondary table?

Also at the Hibernate Forum

+3
source share
1

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

UPDATE

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

    }

}
+5

Source: https://habr.com/ru/post/1774311/


All Articles