-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to ignore some attributes of an @Embeddable
PostPosted: Wed Dec 30, 2015 2:21 pm 
Newbie

Joined: Fri Aug 01, 2014 5:54 am
Posts: 13
Location: Brazil
I have two @Entity A and B, and I have an @Embeddadble Address as follows:

Code:
@Embeddable
public class Address {
    private String line1;
    private String line2;
    ...
}

Code:
@Entity
public class A {
    @Id private long id;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="line1", column=@Column(name="TABLE_A_LINE1")),
        @AttributeOverride(name="line2", column=@Column(name="TABLE_A_LINE2"))
    })
    private Address address;
}

Code:
@Entity
public class B {
    @Id private long id;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="line1", column=@Column(name="TABLE_B_LINE1"))
        // don't want line2 in B
    })
    private Address address;
}

The table for @Entity B has no TABLE_B_LINE2 column, but I still want to embed Address in B because it makes sense in my model. The mapping above doesn't work for B because the SQLs generated by Hibernate always reference line2 column.

If I put @Transient in Address.line2, it works for @Entity B, but now @Entity A loses its capacity to query, insert and update line2.

Do you think it's possible to selectively ignore some attributes of an @Embeddable?


Top
 Profile  
 
 Post subject: Re: How to ignore some attributes of an @Embeddable
PostPosted: Wed Dec 30, 2015 3:12 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The only possible way is if you have a LineOneAddress base class which is extended by Address. The address will get the line1 from the base embeddable while adding the line2.

This way you can choose whether to use the base embeddable or the embeddable one, based on the context


Top
 Profile  
 
 Post subject: Re: How to ignore some attributes of an @Embeddable
PostPosted: Wed Dec 30, 2015 6:10 pm 
Newbie

Joined: Fri Aug 01, 2014 5:54 am
Posts: 13
Location: Brazil
Worked like a charm.
Plus, the object model became more correct. Each address has its singularities.

thank you,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.