|
查看: 1429|回复: 3
|
EJB3 里的OneToMany Annotation 的 mappedBy
[复制链接]
|
|
|
我在跟着一个tutorial作 ,遇到的问题一个问题
Tutorial 的link
http://www.laliluna.de/ejb-3-struts-tutorial-jboss.html
里面有用到
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="customer", targetEntity=Book.class)
请问这个mappedBy="<color=red>customer</color>" 的customer 是指什么? table 里的field name 还是class 里的attribute(variable) ,还是什么?
我没有完全用它的customer-book, 是用自己另外作的class material-materialtype 来作, 所以到了这边有点迷惘。
我尝试找了google , 还是理不出个所以。所以想求助大家
material table
===========
id
code
description
id_materialtype
...etc
materialtype table
==============
id
type
description
..etc
relationship
============
material.id_materialtype = materialtype.id |
|
|
|
|
|
|
|
|
|
|
发表于 4-4-2008 10:29 AM
|
显示全部楼层
我不常用JPA,但是基本的曾经接触过,所以大概知道一些。mappedBy 是指 owner side的property name。Property name需要遵循JavaBean的标准,所以需要declare一个getter才有效。
例如:
- @Entity
- public class Parent {
- .....
- @OneToMany(mappedBy="parent") //EJB会查找Child类里的getParent()方法
- public List<Child> getChildren(){
- ...
- }
- }
- @Entity
- public class Child {
- ...
- @ManyToOne
- @JoinColumn("parent_fk")
- public Parent getParent(){ //<--- mappedBy="parent" 只得就是这个property
- ...
- }
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 4-4-2008 01:28 PM
|
显示全部楼层
mappedBy 内必须放己方(一对多)在对方(多对一)的变量名。
例子:
class Parent{
@OneToMany(mappedBy = "parent")
private Set<Child> children;
}
这里的parent对应
class Child {
@ManyToOne
private Parent parent;
}
我特用相同颜色来使您更加明白。
[ 本帖最后由 amadeus82 于 4-4-2008 01:30 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-4-2008 08:19 PM
|
显示全部楼层
多谢大家,这个问题已经解决。
还在EJB3 挣扎中, JNDI 等问题弄得我头昏脑涨。 以前用JBoss + EJB2的时候还能顺利做到最简单的EJB tutorial , 现在用Geronimo openejb openjpa 困扰了我很多天了, 一个简单的用JNDI lookup EJB 的tutorial 到现在还没完成。
对那些error msg 又不熟悉,不知道是geronimo 还是ejb 还是java classpath 的问题, 目前在很辛苦的阶段,希望过了这关功力会大增。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|