NutzCN Logo
问答 哪位大神有Nutz中一对多映射的Demo,借参考一下,谢谢
发布于 2173天前 作者 Reyco 1284 次浏览 复制 上一个帖子 下一个帖子
标签:

以下是Company 和 Employee 的两个实体,想要实现之间的增删改查,有看技术文档,但是还是不太明白如何去操作

@Table("t_company")
public class Company implements IEntity<Long> {

    @Id(auto = false)
    private Long id;

    @Name
    private String name;

    @Column
    private String remark;

    @Many(target = Employee.class, field = "companyId")
    private List<Employee> employees;

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}

@Table("t_employee")
public class Employee implements IEntity<Long> {

    @Id(auto = false)
    private Long id;

    @Name
    private String name;

    @Column
    private String remark;

    @Column("comid")
    private int companyId;

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public int getCompanyId() {
        return companyId;
    }

    public void setCompanyId(int companyId) {
        this.companyId = companyId;
    }
}
8 回复

有个比较全的, DaoUpTest, 演示各种用法 (无视DaoUp生成的那部分)

https://gitee.com/nutz/nutz/blob/master/test/org/nutz/dao/util/DaoUpTest.java

@wendal 大佬,麻烦看下我贴的源码,遇到comid一直插入为0

Company 和 Employee 的两个实体

@Table("t_company")
public class Company{

    @Id(auto = false)
    private int id;

    @Name
    @Column
    private String name;

    @Column
    private String remark;

    @Many(field = "companyId")
    private List<Employee> employees;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}
@Table("t_employee")
public class Employee{

    @Id(auto = false)
    private int id;

    @Name
    @Column
    private String name;

    @Column
    private String remark;

    @Column("comid")
    private int companyId;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public int getCompanyId() {
        return companyId;
    }

    public void setCompanyId(int companyId) {
        this.companyId = companyId;
    }
}
进行下边的数据插入,但是数据库中未显示comid,comid一直显示为0


        Company company = new Company();
        company.setName("Nutz");
        company.setRemark("测试");

        List<Employee> employee = new ArrayList<Employee>();
        Employee e = new Employee();
        e.setName("C#");
        e.setRemark("test");
//      e.setCompanyId(company.getId());
        employee.add(e);
        company.setEmployees(employee);
        dao.insertWith(company, "employees");

由于无法回复截图,我把t_company和t_employee中的数据贴出来:
t_company表:
字段为:
id name remark
(1, 'Nutz', '测试');
(2, 'Nutz框架', '测试');
t_employee表:
字段为:
id name remark comid
(1, 'C#', 'test', 0);
(2, 'Jave', 'test', 0);

想要t_employee表中的comid跟t_company表中的id同步插入时相同;

    @Id(auto = false)
    private int id;

两个id都是非自动,好像没试过这种情况,待我测一测

@Id(auto=false) 所以id自行解决, 所以company对象的id=0.

Company有字符主键name, 数据库里面存在与待插入数据相同的值"Nutz".
但, 插入的时候, 是不会自动查找数据库里面中name="Nutz"的记录, nutz没有这种特性
因为不会自动查数据库, 所以不会从数据库读到id等于1.
所以company对象的id依然等于0, 赋值给employee的时候, 也是0

insert完成后, 应该会出现一条 id=0,name="Nutz"的t_company记录

至于为什么name会重复, 那是因为@Name只是告知NutDao, 该字段是唯一,但NutzDao是不会校验的.

t_employee和t_company表,我是已经设为自动递增的

但你写的是@Id(auto=false)呀

OK,去除就可以了,感谢大佬~

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