go4it

just do it

实体bean(六)jpa关系(转)

http://tapestry.javaeye.com/blog/31928

Hibernate annotation的关系定义

一、@OneToOne

Java代码 复制代码

@Entity
public class User{   
private Address address;   

@OneToOne
public Address getAddress() {   
return address;   
    }   

public void setAddress(Address address) {   
this.address = address;   
    }   
}   

@Entity
public class Address{   
private User user;   

@OneToOne(mappedBy = "address")   
public User getUser() {   
return user;   
    }   

public void setUser(User user) {   
this.user = user;   
    }   

}  
@Entity
public class User{
	private Address address;

	@OneToOne
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
}

@Entity
public class Address{
	private User user;

	@OneToOne(mappedBy = "address")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}


1、两边都定义了@OneToOne,但都没有定义mappedBy,则user和address表都会生成到对方的外键,双方都是这个关系的拥有者。
2、两边都定义了@OneToOne,如果user定义了mappedBy,则在address表生成到user的外键,address是这个关系的拥有者;如果address定义了mappedBy,则在user表生成到address的外键,user是这个关系的拥有者。


二、@ManyToOne和@OneToMany

Java代码 复制代码

@Entity
public class Employee {   
private Department department;   

@ManyToOne
public Department getDepartment() {   
return department;   
}   

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

@Entity
public class Department {   
private Set<Employee> employees = new HashSet<Employee>();   

@OneToMany(mappedBy="department")   
public Collection<Employee> getEmployees() {   
return employees;   
}   
public void setEmployees(Collection<Employee> employees) {   
this.employees = employees;   
}   
}  
@Entity
public class Employee {
private Department department;

@ManyToOne
public Department getDepartment() {
return department;
}

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

@Entity
public class Department {
private Set<Employee> employees = new HashSet<Employee>();

@OneToMany(mappedBy="department")
public Collection<Employee> getEmployees() {
return employees;
}
public void setEmployees(Collection<Employee> employees) {
this.employees = employees;
}
}
@ManyToOne中Many指的是本类(也就是声明@ManeyToOne的类),One是指关联类,也就是To前边的对应本类,后边的对应关联类。
如果方法返回的是单数关联类则定义@ManyToOne,例如: 
Java代码 

@ManyToOne
public Department getDepartment() {   
return department;   
}  
@ManyToOne
public Department getDepartment() {
return department;
}

方法返回的是Department,为单数关联类,对应@ManyToOne中的One;
如果返回的是复数关联类则定义@OneToMany,例如:

Java代码 复制代码

@OneToMany(mappedBy="department")   
public Collection<Employee> getEmployees() {   
return employees;   
}  
@OneToMany(mappedBy="department")
public Collection<Employee> getEmployees() {
return employees;
}

方法返回的是Collection<Employee>,复数关联类,对应@OneToMany中的Many。其中定义mappedBy的是@OneToMany,也就是说One这一方是关系的拥有者。Many一方的表中生成到关联类的外键。
三、@ManyToMany

Java代码 复制代码

    @Entity
    public class Book {   
    private Set authors = new HashSet<Author>();   
    
    @ManyToMany
    public Set<Author> getAuthors(){   
    return authors;   
    }   
    
    Public void setAuthors(Set<Author> authors){   
       This.authors = authors;   
    }   
    }   
    
    @Entity
    public class Author {   
    private Set books = new HashSet<Book>();   
    
    
    @ManyToMany(mappedBy="authors")   
    public Set<Book> getBooks(){   
    return books;   
    }   
    
    Public void setBooks(Set<Book> books){   
       This.books = books;   
    }   
    }  
    @Entity
    public class Book {
    private Set authors = new HashSet<Author>();
    
    @ManyToMany
    public Set<Author> getAuthors(){
    return authors;
    }
        
    Public void setAuthors(Set<Author> authors){
       This.authors = authors;
    }
    }
    
    @Entity
    public class Author {
    private Set books = new HashSet<Book>();
    
    
    @ManyToMany(mappedBy="authors")
    public Set<Book> getBooks(){
    return books;
    }
    
    Public void setBooks(Set<Book> books){
       This.books = books;
    }
    }

@ManyToMany会生成中间表,具体表名和字段可以通过@AssociationTable来定义,默认的就可以了,同样关系的非拥有者,需要定义mappedBy属性。

posted on 2009-01-18 12:15  cxccbv  阅读(1262)  评论(0)    收藏  举报

导航