4

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() {
        return 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

1 Answer 1

5

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

    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for you answer. Would you mind to give me another suggestion, unfortunately I forgot to specify one part? I can't simply put the table attribute in the @Column annotation since it's reused in another class. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.