I have the following embeddable classes.

Email:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Email")
@Embeddable
public class Email {

     @XmlElement(required = true, nillable = true, name = "etype")
     private String type;

     private String address;
     private String source;

     // getters and setters

}

Address:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MyAddress")
@Embeddable
public class MyAddress {

     @XmlElement(required = true, nillable = true, name = "atype")
     private String type;

     private String zip;
     // getters and setters

}

Entity that embeds both of the above.

@Entity
@Table(name = "PERSON")
public class MyPerson {

   @Embedded
   @AttributeOverrides({
             @AttributeOverride(name = "address", column = @Column(name = "E_ADDR")),
             @AttributeOverride(name = "source", column = @Column(name = "E_SRC")) })
   private Email email;


   @Embedded
   @AttributeOverrides({
        @AttributeOverride(name = "zip", column = @Column(name = "ZIP")),
   private MyAddress address;

}

There is a type field in both of the Embeds. But that field is not mapped to any Database field. But I need to override it. Because hibernate throws error when running. (Compiles fine). Is there a way to override type or give a different name to the embedded and non-database-mapped field?

Note: I am looking for solution with the field name intact. Because I cannot simply change the name.

This would also answer my another question, embedding the same Embeddable again overriding all attributes. For instance, I want to include Home Address, Business Address, etc with same MyAddress embed.

没有正确的解决方案

其他提示

All your fields which are not mapped to the database should be annotated with @Transient. In this case Hibernate will not try to map the field type and will not complain for a duplicate name.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top