0

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.

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173

1 Answers1

1

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.

willome
  • 3,062
  • 19
  • 32
  • The issue is, regardless of Transient or not, `type` is included twice . I just need to find a way to override its name in the main entity. – Kevin Rave Jul 22 '13 at 23:16
  • "There is a type field in both of the Embeds. But that field is not mapped to any Database field" Hibernate is trying to map all your fields (except those annotated with @Transient) and complain with the type field. Finally maybe you want the field type persisted ? – willome Jul 23 '13 at 08:28
  • I don't want that field persisted – Kevin Rave Jul 23 '13 at 13:01
  • If you do not want to persist that field, you have no choice : you need to mark it as @Transient. And your error will magically disappear. – willome Jul 23 '13 at 13:13
  • Even if that is embedded twice? – Kevin Rave Jul 23 '13 at 13:19
  • even if it is embedded : as it is marked @Transient, Hibernate will not try to do anything with it. So Hibernate will not complain with a duplicate field. – willome Jul 23 '13 at 13:27