NutzCN Logo
问答 关于nutz一对一映射的问题
发布于 2715天前 作者 qq_412bd180 1753 次浏览 复制 上一个帖子 下一个帖子
标签:

实体类

public class Company {

    @Id
    private int id;

    @Name
    private String name;

    @Column
    private int ceoId;

    @One(target = Employee.class, field = "ceoId")
    private Employee CEO;

}
public class Employee {
	@Id
	private int id;

	@Name
	private String name;
	
	@Column
	private int age;

}

module

@At
	public void testInsertWith(){
		Company c=new Company();
		c.setName("Apple");
		Employee e=new Employee();
		e.setName("steve");
		e.setAge(40);
		c.setCEO(e);
		//插入关联数据
		dao2.insertWith(c,"CEO");
	}

这是没问题的 但是如果我想把Employee 改造成这个样子

public class Employee {
	@Id
	private int id;

	@Name
	private String name;
	
	@Column
    private int ceoId;
	
	@Column
	private int age;

}

让Employee Id自增 Company表的ceoId字段对应Employee 表的ceoId字段 如何实现呢?

8 回复
@One(field="ceoId", key="ceoId")

实体类

@Table("tt_company")
public class Company {

    @Id
    private int id;

    @Name
    private String name;

    @Column
    private int ceoId;

    @One(target = Employee.class, field = "ceoId",key="ceoId")
    private Employee CEO;

...
@Table("tt_employee")
public class Employee {
	@Id
	private int id;

	@Name
	private String name;
	
	@Column
    private int ceoId;
	
	@Column
	private int age;

module

@At
	public void testInsertWith(){
		Company company=new Company();
		company.setName("yili");
		Employee employee=new Employee();
		employee.setName("lee");
		employee.setAge(30);
		company.setCEO(employee);
		dao2.insertWith(company,"CEO");
	}

插入后两个表的coeId都是0

关联属性并非主键, 推导不出ceoId的值是多少的,那只能自行设置

能结合@Pre来设置吗

为啥不跟Employee 的id进行关联呢?? 即使是插入之后, Employee 的ceoId依然没有值的

是不是以为@Id只能标注到名字为id的属性??

添加回复
请先登陆
回到顶部