Redian新闻
>
再问一个OR Mapping的问题
avatar
再问一个OR Mapping的问题# Java - 爪哇娇娃
A*o
1
一个Customer Entity,有两个Set,分别叫privateEmails和publicEmails,
里面都是Email。难道就要映射到两个Email表上去?还有没有更简洁的方案?
谢谢!
avatar
g*g
2
映射到同一个email表上有啥问题?

【在 A**o 的大作中提到】
: 一个Customer Entity,有两个Set,分别叫privateEmails和publicEmails,
: 里面都是Email。难道就要映射到两个Email表上去?还有没有更简洁的方案?
: 谢谢!

avatar
A*o
3
same forgein key on email table,
how can you tell it's private or public?

【在 g*****g 的大作中提到】
: 映射到同一个email表上有啥问题?
avatar
b*y
4
不会吧,难道foreign key跟哪个field都不能specify?
逼你去duplicate email table?
老夫觉得难以相信啊

【在 A**o 的大作中提到】
: same forgein key on email table,
: how can you tell it's private or public?

avatar
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{
}
avatar
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{
: }

avatar
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.

【在 A**o 的大作中提到】
: 一个Customer Entity,有两个Set,分别叫privateEmails和publicEmails,
: 里面都是Email。难道就要映射到两个Email表上去?还有没有更简洁的方案?
: 谢谢!

avatar
A*o
8
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.

avatar
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.

avatar
t*e
10
@OneToMany如果能加where clause就最好了,没看到这样的例子。 @
DiscriminatorValue里面是可以加where clause的.
avatar
g*g
11
+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.

avatar
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.

avatar
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.

avatar
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",
avatar
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

avatar
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")
: )

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。