这人气好,一个cmp问题,请帮忙,只剩70分了!

forumcsdn 2003-10-09 03:28:26
一对多的关系,两张表如下:

create table teacher(
teaID int not null primary key,
teaName varchar(20)
)

create table teacherInfo (
infoID int not null primary key,
teaID int not null,
infoDesc varchar(20)
constraint teaID_FK foreign key(teaID) references teacher(teaID)
)

以下代码全部在 jbuilder8和weblogic7中生成并运行:

(cmp)TeacherBean中:
public java.lang.Integer ejbCreate(Integer teaID, String teaName) throws CreateException {
setTeaID(teaID);
setTeaName(teaName);
return null;
}
public void ejbPostCreate(Integer teaID, String teaName) throws CreateException {
}



(cmp)TeacherInfoBean中:
public java.lang.Integer ejbCreate(Integer infoID,Integer teaID, String infoDesc) throws CreateException {
setInfoID(infoID);
setInfoDesc(infoDesc);
return null;
}
public void ejbPostCreate(Integer infoID,Integer teaID, String infoDesc) throws CreateException {
setTeaID(teaID);
}


sessonBean中调用上面的个cmp :
public void addTeacher(Integer teaID,String teaName,Integer infoID,String infoDesc) {
try {
Context ctx = new InitialContext();
TeacherHome teacherHome = (TeacherHome)ctx.lookup("Teacher");
TeacherInfoHome teacherInfoHome = (TeacherInfoHome)ctx.lookup("TeacherInfo");
Teacher teacher = teacherHome.create(teaID,teaName);
TeacherInfo teacherInfo = teacherInfoHome.create(infoID,teaID,infoDesc);
teacherInfo.setTeacher(teacher);
}catch(Exception e){
e.printStackTrace();
}
}

报错为:
javax.ejb.TransactionRolledbackLocalException: EJB Exception:; nested exception
is: javax.ejb.EJBException: When a cmp-field and a cmr-field (relationship) are
mapped to the same column, the setXXX method for the cmp-field may not be called
. The cmp-field is read-only.


应该怎么解呢????请指点,谢谢!!!!马上送分!!!
...全文
47 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
asdmonster 2003-10-09
  • 打赏
  • 举报
回复
create table teacherInfo (
infoID int not null primary key,
teaID int not null,
infoDesc varchar(20)
constraint teaID_FK foreign key(teaID) references teacher(teaID)
)

===========================================================================
这个表的 teaID 不能为非空,因为在cmp中这个字段是不允许被操作的,相应的应该操作其对应的cmr字段(如果你没有更改的话应该是eacherInfoBean的teacher字段)。
这一点上,除非你不用cmp,否则没有任何变通的地方(一种变通是你的teacher字段属于ejbCreate()的参数,但是teaID必须为可空)

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧