@OneToMany与@ManyToMany

==========User.java=========
package com.me.model;

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

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "T_USER", schema = "SSHE")
public class User implements java.io.Serializable {

// Fields
private String id;
private String name;
private String pwd;
private Date createdatetime;
private Date modifydatetime;
private Set
addresses = new HashSet
();
private Set roles = new HashSet();

// Constructors

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

/** minimal constructor */
public User(String id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}

/** full constructor */
public User(String id, String name, String pwd, Date createdatetime,
Date modifydatetime) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.createdatetime = createdatetime;
this.modifydatetime = modifydatetime;
}

// Property accessors
@Id
@Column(name = "ID", unique = true, nullable = false, length = 36)
public String getId() {
return this.id;
}

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

@Column(name = "NAME", nullable = false, length = 100)
public String getName() {
return this.name;
}

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

@Column(name = "PWD", nullable = false, length = 32)
public String getPwd() {
return this.pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATEDATETIME", length = 7)
public Date getCreatedatetime() {
return this.createdatetime;
}

public void setCreatedatetime(Date createdatetime) {
this.createdatetime = createdatetime;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "MODIFYDATETIME", length = 7)
public Date getModifydatetime() {
return this.modifydatetime;
}

public void setModifydatetime(Date modifydatetime) {
this.modifydatetime = modifydatetime;
}

@OneToMany(cascade = CascadeType.ALL, mappedBy = "contactName", fetch = FetchType.EAGER)
public Set
getAddresses() {
return addresses;
}

public void setAddresses(Set
addresses) {
this.addresses = addresses;
}

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID",referencedColumnName = "id")}, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
public Set getRoles() {
return roles;
}

public void setRoles(Set roles) {
this.roles = roles;
}

}


=====================Role.java

package com.me.model;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "T_ROLE", schema = "SSHE")
public class Role implements java.io.Serializable {
private int id;

private String role;

private Set users;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}

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

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

// @ManyToMany(fetch = FetchType.LAZY, mappedBy = "students")

@ManyToMany(fetch = FetchType.LAZY,mappedBy = "roles")
public Set getUsers() {
return users;
}

public void setUsers(Set users) {
this.users = users;
}
}

====================Address.java================
package com.me.model;

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

@Entity
@Table(name = "T_ADDRESS", schema = "SSHE")
public class Address implements java.io.Serializable {
private User contactName;
private String postCode;
private String detail;
private String phone;
private Integer id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}

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

@ManyToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "USER_ID")
public User getContactName() {
return contactName;
}

public void setContactName(User contactName) {
this.contactName = contactName;
}

@Column(name = "CODE", length = 50)
public String getPostCode() {
return postCode;
}

public void setPostCode(String postCode) {
this.postCode = postCode;
}

@Column(name = "DETAIL", length = 200)
public String getDetail() {
return detail;
}

public void setDetail(String detail) {
this.detail = detail;
}

@Column(name = "PHONE_NUM", length = 200)
public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

}


===================UserServiceImpl.java=============
package me.gacl.service.impl;


import java.io.Serializable;
import java.util.List;

import me.gacl.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.me.dao.UserDao;
import com.me.model.User;
//使用Spring提供的@Service注解将UserServiceImpl标注为一个Service
@Service("userService")
public class UserServiceImpl implements UserService {
/**
* 注入userDao
*/
@Autowired
private UserDao userDao;

public void test() {
System.out.println("Hello World!");
}

public Serializable save(User user) {
return userDao.save(user);
}

public List getUsers() {
return userDao.getUsers();
}


}
===================UserDaoImpl.java==================

package com.me.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.me.dao.UserDao;
import com.me.model.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao {

/**
* 使用@Autowired注解将sessionFactory注入到UserDaoImpl中
*/
@Resource
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public Serializable save(User user) {
return sessionFactory.getCurrentSession().save(user);
}

public List getUsers() {
Session session= sessionFactory.openSession();
return session.createQuery("from User").list();

}
}

=================TestHiberante.java==================
package com.me.test;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import me.gacl.service.UserService;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.me.model.Address;
import com.me.model.Role;
import com.me.model.User;

public class TestHibernate {

private UserService userService;

/**
* 这个before方法在所有的测试方法之前执行,并且只执行一次
* 所有做Junit单元测试时一些初始化工作可以在这个方法里面进行
* 比如在before方法里面初始化ApplicationContext和userService
*/
@Before
public void before(){
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"});
userService = (UserService) ac.getBean("userService");
}

@Test
public void testSaveMethod(){
//ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"});
//UserServiceI userService = (UserServiceI) ac.getBean("userService");
User user = new User();
user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
user.setName("gacl");
user.setPwd("123");
user.setCreatedatetime(new Date());
userService.save(user);
}
@Test
public void testFindMethod(){
//ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"});
//UserServiceI userService = (UserServiceI) ac.getBean("userService");

List list=userService.getUsers();
System.out.println(list.size());
}
@Test
public void testOtMSaveMethod(){
//ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"});
//UserServiceI userService = (UserServiceI) ac.getBean("userService");
User user = new User();
user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
user.setName("gacl");
user.setPwd("123");
user.setCreatedatetime(new Date());
Address a=new Address();
a.setContactName(user);
a.setDetail("Sz xxx");
a.setPhone("18956264598");
a.setPostCode("898989");
Address ab=new Address();
ab.setContactName(user);
ab.setDetail("Sz xxx12");
ab.setPhone("18956264598");
ab.setPostCode("888989");
user.getAddresses().add(ab);
user.getAddresses().add(a);
userService.save(user);
}
@Test
public void testGetsMethod(){
//ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"});
//UserServiceI userService = (UserServiceI) ac.getBean("userService");

List list=userService.getUsers();
for (User user : list) {
System.out.println(user.getName());
Set
set=user.getAddresses();
for (Address address : set) {
System.out.println(address.getPhone());
}
}
}
@Test
public void testMTM(){
User user = new User();
user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
user.setName("comMe");
user.setPwd("123");
user.setCreatedatetime(new Date());
Role r=new Role();
r.setRole("Adminstrator");
Role r1=new Role();
r1.setRole("Hr");
user.getRoles().add(r1);
user.getRoles().add(r);
userService.save(user);
}
}

你可能感兴趣的:(SSH,测试,java)