请问hibernate在一对多关系中在一的一端定义mappedby具体是什么意思呢,用crud举例一下呢,谢谢

比如department和employee是一对多关系的,我在department那边设置了mappedby,最后测试的结果只是不会生成第三张多余的表,其他没感觉什么,网上都说让多的这一段作为关系维护端,可是设置了mappedby后我还是可以在一的一端维护关系啊,那hibernate具体是怎么处理的呢,能不能从代码的角度再具体一点呢,谢谢

4个回答

实体配置如下:

[code="java"]package com;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**

  • Department entity.
  • @author MyEclipse Persistence Tools
    */
    @Entity
    @Table
    public class Department implements java.io.Serializable {

    // Fields
    @Id
    private Long depid;
    private String depname;
    @OneToMany(mappedBy="department")
    private Set employees = new HashSet();
    // Constructors

    /** default constructor */
    public Department() {
    }

    /** minimal constructor */
    public Department(Long depid) {
    this.depid = depid;
    }

    /** full constructor */
    public Department(Long depid, String depname) {
    this.depid = depid;
    this.depname = depname;
    }

    // Property accessors

    public Long getDepid() {
    return this.depid;
    }

    public void setDepid(Long depid) {
    this.depid = depid;
    }

    public String getDepname() {
    return this.depname;
    }

    public void setDepname(String depname) {
    this.depname = depname;
    }

    public Set getEmployees() {
    return employees;
    }

    public void setEmployees(Set employees) {
    this.employees = employees;
    }

}[/code]
[code="java"]package com;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**

  • Employee entity.
  • @author MyEclipse Persistence Tools */ @Entity @Table

public class Employee implements java.io.Serializable {

// Fields
@Id
private Long eeid;
private String eename;
@Column(name="dep_id",insertable=false,updatable=false)
private Long depId;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="dep_id")
private Department department;
// Constructors

/** default constructor */
public Employee() {
}

/** minimal constructor */
public Employee(Long eeid) {
    this.eeid = eeid;
}

/** full constructor */
public Employee(Long eeid, String eename, Long depId) {
    this.eeid = eeid;
    this.eename = eename;
    this.depId = depId;
}

// Property accessors

public Long getEeid() {
    return this.eeid;
}

public void setEeid(Long eeid) {
    this.eeid = eeid;
}

public String getEename() {
    return this.eename;
}

public void setEename(String eename) {
    this.eename = eename;
}

public Long getDepId() {
    return this.depId;
}

public void setDepId(Long depId) {
    this.depId = depId;
}

public Department getDepartment() {
    return department;
}

public void setDepartment(Department department) {
    this.department = department;
}

}[/code]

:( 请lz还是把自己的源码贴出来,让大家分析下

mapped用于指定在双向关系中两个实体中被哪个实体是被关联处理的.

在你的department和employee的一对多关系中,当你指定department中的mappedBy后,你的关系只能被employee来主动维护.也就是employee级联的处理department.

一对多单向关联(多方维护):
[code="java"]
package com;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;

/*

  • created time:上午11:44:11
  • creator: Tian Zhichao (chinoistzc@gmail.com)
    *
    */
    public class Test {

    /**

    • main()
    • 2010-4-5,上午11:44:11
      */
      public static void main(String[] args) {
      // TODO Auto-generated method stub
      Session session = HibernateSessionFactory.getSession();
      session.beginTransaction();
      Department dep = new Department();

      dep.setDepid(123L);
      dep.setDepname("dep name");
      Employee ee1 = new Employee();
      Employee ee2 = new Employee();
      ee1.setEeid(1L);
      ee1.setDepartment(dep);
      ee2.setEeid(2L);
      ee2.setDepartment(dep);
      Set employees = new HashSet();
      employees.add(ee1);
      employees.add(ee2);
      dep.setEmployees(employees);
      session.save(ee1);
      session.save(ee2);
      session.getTransaction().commit();
      System.out.println("-----------------用一端来保存时....");
      session.beginTransaction();
      Department dep2 = new Department();
      dep2.setDepid(100L);
      dep2.setEmployees(employees);
      session.save(dep2);
      session.getTransaction().commit();
      session.close();
      }

}

[/code]
Hibernate打印输出的:
[code="java"]log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
select
department_.depid,
department_.depname as depname1_
from
Department department_
where
department_.depid=?
Hibernate:
insert
into
Department
(depname, depid)
values
(?, ?)
Hibernate:
insert
into
Employee
(dep_id, eename, eeid)
values
(?, ?, ?)
Hibernate:
insert
into
Employee
(dep_id, eename, eeid)
values
(?, ?, ?)
-----------------用一端来保存时....
Hibernate:
insert
into
Department
(depname, depid)
values
(?, ?)
[/code]

下面这个显示了只department,做为被维护端时,如果保存它,Hibernate会只保存它自已.
[code="java"]
package com;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;

/*

  • created time:上午11:44:11
  • creator: Tian Zhichao (chinoistzc@gmail.com)
    *
    */
    public class Test {

    /**

    • main()
    • 2010-4-5,上午11:44:11
      */
      public static void main(String[] args) {
      // TODO Auto-generated method stub
      Session session = HibernateSessionFactory.getSession();
      session.beginTransaction();
      Department dep = new Department();

      dep.setDepid(123L);
      dep.setDepname("dep name");
      Employee ee1 = new Employee();
      Employee ee2 = new Employee();
      ee1.setEeid(1L);
      ee1.setDepartment(dep);
      ee2.setEeid(2L);
      ee2.setDepartment(dep);
      Set employees = new HashSet();
      employees.add(ee1);
      employees.add(ee2);
      dep.setEmployees(employees);
      session.save(dep);
      session.getTransaction().commit();
      session.close();
      }

}
[/code]
[code="java"]log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
insert
into
Department
(depname, depid)
values
(?, ?)
[/code]

[size=x-large][color=red]希望对你有帮助..[/color][/size]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问