在看官方的文档时,测试了一下一对一映射建表,对应关系是一个pet对应一个master,然后建表插入遇到了如题的问题,以下是我的pojo类:
Pet类,
package demo.hello.pojo;
import org.nutz.dao.entity.annotation.*;
@Table("t_pet")
public class Pet {
@Id(auto=true) // 表示该字段为一个自增长的Id,注意,是数据库表中自增!!
//@Prev(els=@EL("$me.getId()"))
//@Next(@SQL("SELECT t_pet('id')"))
private Integer id;// @Id与属性名称id没有对应关系
@Column
private String pname;
@Column
private Integer age;
@Column
@Prev(@SQL ("SELECT photoPath from t_pet where pname = @pname"))
private String photoPath;
@Column
private int masterId;
@One(target=Master.class,field="masterId")
private Master master;
public int getMasterId() {
return masterId;
}
public void setMasterId(int masterId) {
this.masterId = masterId;
}
public Master getMaster() {
return master;
}
public void setMaster(Master master) {
this.master = master;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhotoPath() {
return photoPath;
}
public void setPhotoPath(String photoPath) {
this.photoPath = photoPath;
}
@Override
public String toString() {
return "Pet [id=" + id + ", pname=" + pname + ", age=" + age + ", photoPath=" + photoPath + ", masterId="
+ masterId + ", master=" + master + "]";
}
}
Master类:
package demo.hello.pojo;
import org.nutz.dao.entity.annotation.*;
@Table("t_master")
public class Master {
@Id
private Integer masterId;
@Column
private String name;
@Column
private Integer age;
public Integer getMasterId() {
return masterId;
}
public void setMasterId(Integer masterId) {
this.masterId = masterId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Master [masterId=" + masterId + ", name=" + name + ", age=" + age + "]";
}
}
第一次插入,成功且没有问题:
dao.create(Master.class, false);
Pet pet = new Pet();
pet.setPname("tomcat");
Master master = new Master();
master.setName("chris");
dao.insertWith(pet, "master");