用着用着,突然发觉EasyDBO并没有实现到它声称的功能。
如,当你的实体有一对一关联或者一对多等关联时,它并没有能够正确地处理外键值(当关联的主键为自增类型时,插入关联对象获取到外键为空,被关联的对象虽然能插入,但是这样有舍用呢?)。
看看它处理的过程就知道它不能正确出来外键值了。
举个具体的例子吧。
关联实体:
@Table(tableName="customer",keyField="cid",keyGenerator="com.easyjf.dbo.NullIdGenerator")
public class Customer implements Serializable {
//EasyJWeb Tools自动代码生成
//属性部份
/** *//**
*
*/
private static final long serialVersionUID = 1L;
@TableField(name="cid")
private String cid;
@TableField(name="userName")
private String username;
@TableField(name="password")
private String password;
@TableField(name="email")
private String email;
@TableField(name="tel")
private String tel;
@TableField(name="birthday")
private Date birthday;
@TableField(name="intro")
private String intro;
@OneToOne(column="messageId",tableName="message",type=Message.class,key="cid")
private Message message;
@ManyToOne(column="messageId",tableName="",type=Message.class,key="cid")
private List<Message> massages;
//getter及setter方法
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
public Message getMessage()
{
return message;
}
public void setMessage(Message message)
{
this.message = message;
}
public List<Message> getMassages()
{
return massages;
}
public void setMassages(List<Message> massages)
{
this.massages = massages;
}
}
被关联实体:
@Table(tableName="message",keyField="cid",keyGenerator="com.easyjf.dbo.NullIdGenerator")
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
@TableField(name="cid")
private Long cid;
@TableField(name="title")
private String title;
@TableField(name="content")
private String content;
@TableField(name="inputUser")
private String inputuser;
@TableField(name="inputTime")
private java.util.Date inputtime;
@TableField(name="publish")
private Integer publish;
@TableField(name="status")
private Integer status;
public Long getCid() {
return cid;
}
public void setCid(Long cid) {
this.cid = cid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getInputuser() {
return inputuser;
}
public void setInputuser(String inputuser) {
this.inputuser = inputuser;
}
public java.util.Date getInputtime() {
return inputtime;
}
public void setInputtime(java.util.Date inputtime) {
this.inputtime = inputtime;
}
public Integer getPublish() {
return publish;
}
public void setPublish(Integer publish) {
this.publish = publish;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
测试:
public class Test
{
public static void main(String[] args)
{
// addAData();
addDatas();
}
public static void addDatas(){
EasyJDB db=EasyJDB.getInstance();
db.setAutoCommit(false);
Customer customer = new Customer();
customer.setUsername("sometwo");
customer.setBirthday(new Date());
customer.setIntro("某某人");
customer.setCid(System.currentTimeMillis()+"");
Message message1 = new Message();
message1.setTitle("a message");
message1.setContent("某某信息.");
message1.setInputtime(new Date());
Message message2 = new Message();
message2.setTitle("another message");
message2.setContent("另某某信息.");
message2.setInputtime(new Date());
List<Message> list = new ArrayList<Message>();
list.add(message1);
list.add(message2);
customer.setMassages(list);
db.add(customer);
db.commit();
db.close();
}
public static void addAData(){
EasyJDB db=EasyJDB.getInstance();
db.setAutoCommit(false);
Customer customer = new Customer();
customer.setUsername("someone");
customer.setBirthday(new Date());
customer.setIntro("某人");
customer.setCid(System.currentTimeMillis()+"");
Message message1 = new Message();
message1.setTitle("a message");
message1.setContent("某某信息.");
message1.setInputtime(new Date());
customer.setMessage(message1);
db.add(customer);
db.commit();
db.close();
}
}
调试信息输出:
EasyDBO:insert into customer(password,userName,intro,email,tel,cid,birthday) values(?,?,?,?,?,?,?)
EasyDBO:insert into message(inputUser,title,content,status,inputTime,cid,publish) values(?,?,?,?,?,?,?)
EasyDBO:insert into message(inputUser,title,content,status,inputTime,cid,publish) values(?,?,?,?,?,?,?)
数据库的结果是,记录插入数据库了,只是对应的外键(customer表的messageId为空!这个是致命bug啊!)
看,调试信息反映出来的对象持久化顺序是先插入关联对象customer,然后再插入被关联对象message(或者集合messages)。
当插入customer时,它去获取被关联对象的主键,即message.id,但是message此时为空!customer数据库表的messageId字段只能为空了。
当插入被关联对象message时,由于id是自增型的,它只能在数据库里完成id的填充,但是当整条记录插入后,它又没有同message对象关联起来,即没有hibernate所说的状态转换(游离状态等)。
然后就完了,不负责地留下未完成的数据持久使命......
要填补这个bug,简单的办法就是将插入对象的顺序调过来,即先插入被关联对象(message),将生成的主键回传给对象,再插入关联对象(customer)。应该可以吧,想法如此,还没有作修改实现。
这么多bug,想放弃用EasyDBO了,连生成代码工具都写好了,却在这时放弃........真不够意思~~~~~~
持久层,还是jpa吧.......国产的开源啊,要负责一些,争气些,不是不想支持,而是实在.........
posted on 2007-06-28 23:10
三告习习 阅读(1228)
评论(4) 编辑 收藏 所属分类:
easyJF-projects