【在 A**o 的大作中提到】 : same forgein key on email table, : how can you tell it's private or public?
t*e
5 楼
table per hierarchy. Define an abstract entity - Email. @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn( name="Inheritance", discriminatorType=DiscriminatorType.STRING ) public astract class Email{ } followed by, @Entity @DiscriminatorValue("Public") public class PublicEmail{ } @Entity @DiscriminatorValue("Private") public class PrivateEmail{ }
A*o
6 楼
Thank you! How do you map the one to many to Customer, then? Does it mean that I map one to PublicEmail and another to PrivateEmail? That looks cool.
【在 t*******e 的大作中提到】 : table per hierarchy. : Define an abstract entity - Email. : @Entity : @Inheritance(strategy = InheritanceType.SINGLE_TABLE) : @DiscriminatorColumn( : name="Inheritance", : discriminatorType=DiscriminatorType.STRING : ) : public astract class Email{ : }
m*t
7 楼
Add a TYPE field to the email table. And use the 'where' attribute on the set mapping to set the filtering condition.
Any sample (pseudo) code? or the annotation i should look for? i like totempole's solution for now.
【在 m******t 的大作中提到】 : : Add a TYPE field to the email table. And use the 'where' attribute on the : set mapping to set the filtering condition.
m*t
9 楼
Not sure about the equivalent annotation. I don't use those for OR mapping. That's certainly another way of doing it. I like the where approach because I feel it's a bit of an overkill to introduce different classes only for filtering purposes - kind of like bending the OO design backwards to fit into the ORM tool. But it's really a personal taste thing I guess.
【在 A**o 的大作中提到】 : Any sample (pseudo) code? or the annotation i should look for? : i like totempole's solution for now.
+1, not worth the trouble when a simple wrapper in DAO can do it.
【在 m******t 的大作中提到】 : : Not sure about the equivalent annotation. I don't use those for : OR mapping. : That's certainly another way of doing it. I like the where approach : because I feel it's a bit of an overkill to introduce : different classes only for filtering purposes - kind of like bending : the OO design backwards to fit into the ORM tool. But it's really : a personal taste thing I guess.
F*n
12 楼
Actually they are same. When you declare "one table per hierarchy" it will create a special "descriminant" column in the table to tell which subclass the data record belongs to. This is equivalent to your explicit "type" column -- it is just managed transparently to developers.
【在 m******t 的大作中提到】 : : Not sure about the equivalent annotation. I don't use those for : OR mapping. : That's certainly another way of doing it. I like the where approach : because I feel it's a bit of an overkill to introduce : different classes only for filtering purposes - kind of like bending : the OO design backwards to fit into the ORM tool. But it's really : a personal taste thing I guess.
A*o
13 楼
yeah. that transparency is what i like. less work for me. :)
【在 F****n 的大作中提到】 : Actually they are same. When you declare "one table per hierarchy" it will : create a special "descriminant" column in the table to tell which subclass : the data record belongs to. This is equivalent to your explicit "type" : column -- it is just managed transparently to developers.
A*o
14 楼
OK, after reading Hibernate doc, I figured the Unidirectional with join table strategy suits my case best. Because the email is a common object and shared across the objects. The follow is quote from hibernate: http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html A unidirectional one to many with join table is much preferred. This association is described through an @JoinTable. @Entity public class Trainer { @OneToMany @JoinTable( name="TrainedMonkeys",
t*e
15 楼
This is a good choice. In Hibernate, you may instead use collections of components because theoretically email/address are embeddable objects (2nd class entities), @CollectionOfElements( targetElement = java.lang.String.class ) @JoinTable( name = "UserEmails", [email protected](name="UserId") ) @Column(name="Email", nullable=false) @OrderBy(clause="Email ASC") private Set emails; Without a join table, the reverse pointer/foreign k
【在 A**o 的大作中提到】 : OK, after reading Hibernate doc, I figured the Unidirectional with join : table strategy suits my case best. Because the email is a common object and : shared across the objects. : The follow is quote from hibernate: : http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html : A unidirectional one to many with join table is much preferred. This : association is described through an @JoinTable. : @Entity : public class Trainer { : @OneToMany
A*o
16 楼
i'm stuck on jboss ejb3 for the moment, no hibernate extenstions for now. but since it's based on hibernate, i just read their doc for more info. again, thanks for your help, all of you who replied.
【在 t*******e 的大作中提到】 : This is a good choice. : In Hibernate, you may instead use collections of components because theoretically : email/address are embeddable objects (2nd class entities), : @CollectionOfElements( : targetElement = java.lang.String.class : ) : @JoinTable( : name = "UserEmails", : [email protected](name="UserId") : )