在desploy下创建数据源mysql-ds.xml
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>zynDS</jndi-name> <use-java-context>false</use-java-context> <connection-url>jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>admin</password> <exception-sorter-class-name>org. jboss.resource.adapter.jdbc.vendor. MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources>
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="zyn"> <jta-data-source>zynDS</jta-data-source> <properties> <property name="show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence>
@Entity @Table(name = "student") public class Student extends DomainObject { /** * */ private static final long serialVersionUID = 9090499971596826674L; public Student() { } public Student(String name, int age, Long num) { this.name = name; this.age = age; this.num = num; } /** * 业务主键ID,自增,增量1 */ @GeneratedValue(strategy = GenerationType.IDENTITY) @Id private Integer id; /** * 学生姓名 */ @Column(length = 10, name = "student_name", nullable = false) private String name = ""; /** * 学生年龄 */ @Column(length = 2, name = "student_age", nullable = false) private Integer age = 0; /** * 学号 */ @Column(length = 4, name = "student_no", nullable = false) private Long num; @ManyToMany(fetch=FetchType.LAZY,mappedBy="students",cascade={CascadeType.MERGE,CascadeType.PERSIST}) private Set<Teacher> teachers = new HashSet<Teacher>(); @OneToOne(fetch=FetchType.EAGER,mappedBy="student",cascade=CascadeType.ALL,optional=true) private StudentInfo studentInfo ; public StudentInfo getStudentInfo() { return studentInfo; } public void setStudentInfo(StudentInfo studentInfo) { this.studentInfo = studentInfo; } public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } public Long getNum() { return num; } public void setNum(Long num) { this.num = num; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
@SuppressWarnings("serial") @Entity @Table(name = "student_info") public class StudentInfo extends DomainObject { public StudentInfo() { } public StudentInfo(String card, String address) { this.userCard = card; this.address = address; } // 使用当前对象中student属性的主键来作为本对象的主键 @Id @GeneratedValue(generator = "pkGenerator") @GenericGenerator(name = "pkGenerator", strategy = "foreign", parameters = @Parameter(name = "property", value = "student")) private Integer id; /** * 身份证号 */ private String userCard; /** * 详细地址 */ private String address; @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, optional = false) @PrimaryKeyJoinColumn private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserCard() { return userCard; } public void setUserCard(String userCard) { this.userCard = userCard; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
@SuppressWarnings("serial") @Entity @Table(name = "teacher") //@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class Teacher extends DomainObject { public Teacher() { } public Teacher(String name) { this.name = name; } @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "teacher_name",nullable=false) private String name; @ManyToMany(fetch=FetchType.LAZY,cascade={CascadeType.PERSIST,CascadeType.MERGE}) @JoinTable(name = "teacher_student", joinColumns = @JoinColumn(name = "teacher_id",referencedColumnName="id"), inverseJoinColumns = @JoinColumn(name = "student_id",referencedColumnName="id")) @OrderBy("num DESC") private Set<Student> students = new HashSet<Student>(); @OneToMany(mappedBy="teacher",fetch=FetchType.LAZY,cascade={CascadeType.MERGE,CascadeType.REMOVE}) private Set<Course> courses = new HashSet<Course>(); public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public interface StudentDao { public void persist(Student student); public void merge(Student student); public void getAll(); } @Remote(value={StudentDao.class}) @Stateless public class StudentDaoBean implements StudentDao{ @PersistenceContext(unitName="zyn") private EntityManager entityManager ; public void persist(Student student) { this.entityManager.persist(student); } public void merge(Student student) { this.entityManager.merge(student); } public void getAll() { } }
public class StudentDaoTest { private StudentDao studentDao ; private InitialContext context; @Before public void init() { try { context = new InitialContext(); this.studentDao = (StudentDao)this.context.lookup("StudentDaoBean/remote"); } catch (Exception e) { e.printStackTrace(); } } @Test public void save() { Student student = new Student("李四", 22, 20072724L); Teacher teacher1 = new Teacher("王老师"); Teacher teacher2 = new Teacher("李老师"); student.getTeachers().add(teacher1); student.getTeachers().add(teacher2); StudentInfo studentInfo = new StudentInfo("520000000000000000","成都市"); studentInfo.setStudent(student); try { this.infoDao.persist(studentInfo); } catch (Exception e) { e.printStackTrace(); } } }