在java里,在一个类里创建另一个类的对象的作用?和在一个类里指定另一类的引用的作用?例如下面的代码
packagecom.bjsxt.onetomany.po;importjava.util.HashSet;importjava.util.Set;importjavax...
package com.bjsxt.onetomany.po;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity //可以将当前实体类和数据库的表名和字段进行关联映射
//@Table(name="tb_deppartment")指定一个表名,不指定默认
public class Department { //一方 ,主键
@Id
@GeneratedValue(strategy=GenerationType.AUTO) //注解 id的生成策略。AUTO相当于xml生成id的name属性
private int id;
private String dname;
private String location;
//cascade级联 ALL是增删改查,mapped是有哪一方维护外键的值。
//一方设置成由多方维护外键的值 mappedBy,不会生成第三张表
@OneToMany(mappedBy="department",cascade=CascadeType.ALL)
private Set<Employee> employees =new HashSet<Employee>(); //泛型里是类,类的对象装了很多数据,操作
public int getId() {
return id;
}
public String getDname() {
return dname;
}
public String getLocation() {
return location;
}
public void setId(int id) {
this.id = id;
}
public void setDname(String dname) {
this.dname = dname;
}
public void setLocation(String location) {
this.location = location;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
} 展开
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity //可以将当前实体类和数据库的表名和字段进行关联映射
//@Table(name="tb_deppartment")指定一个表名,不指定默认
public class Department { //一方 ,主键
@Id
@GeneratedValue(strategy=GenerationType.AUTO) //注解 id的生成策略。AUTO相当于xml生成id的name属性
private int id;
private String dname;
private String location;
//cascade级联 ALL是增删改查,mapped是有哪一方维护外键的值。
//一方设置成由多方维护外键的值 mappedBy,不会生成第三张表
@OneToMany(mappedBy="department",cascade=CascadeType.ALL)
private Set<Employee> employees =new HashSet<Employee>(); //泛型里是类,类的对象装了很多数据,操作
public int getId() {
return id;
}
public String getDname() {
return dname;
}
public String getLocation() {
return location;
}
public void setId(int id) {
this.id = id;
}
public void setDname(String dname) {
this.dname = dname;
}
public void setLocation(String location) {
this.location = location;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询