分类:
2008-11-17 22:22:25
@ManyToMany(cascade = {CascadeType.CREATE, CascadeType.MERGE}, fetch = FetchType.EAGER, isInverse = true) @AssociationTable(table = @Table(name = "STUDENT_TEACHER"), joinColumns = {@JoinColumn(name = "TEACHER_ID")},inverseJoinColumns = {@JoinColumn(name = "STUDENT_ID")}) @ AssociationTable的注释声明如下: @Target({METHOD, FIELD}) public @interface AssociationTable { Table table() default @Table(specified=false); JoinColumn[] joinColumns() default {}; JoinColumn[] inverseJoinColumns() default {}; } |
@OneToOne(cascade = {CascadeType.ALL}) @JoinColumn(name = "DOSSIER_ID") public Dossier getDossier() { return dossier; } |
@ OneToOne的注释声明如下: @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OneToOne { String targetEntity() default ""; CascadeType[] cascade() default {}; FetchType fetch() default EAGER; boolean optional() default true; } |
package com.kuaff.ejb3.relationships; import javax.ejb.CascadeType; import javax.ejb.Entity; import javax.ejb.FetchType; import javax.ejb.GeneratorType; import javax.ejb.Id; import javax.ejb.JoinColumn; import javax.ejb.OneToOne; import javax.ejb.ManyToMany; import javax.ejb.Table; import javax.ejb.AssociationTable; import java.util.ArrayList; import java.util.Set; import java.util.Collection; import java.io.Serializable; @Entity @Table(name = "STUDENT") public class Student implements Serializable { private int id; private String first; private String last; private Dossier dossier; private Set @Id(generate = GeneratorType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } public void setFirst(String first) { this.first = first; } public String getFirst() { return first; } public void setLast(String last) { this.last = last; } public String getLast() { return last; } public void setDossier(Dossier dossier) { this.dossier = dossier; } @OneToOne(cascade = {CascadeType.ALL}) @JoinColumn(name = "DOSSIER_ID") public Dossier getDossier() { return dossier; } public void setTeacher(Set { this.teachers = teachers; } @ManyToMany(cascade = {CascadeType.CREATE, CascadeType.MERGE}, fetch = FetchType.EAGER, isInverse = true) @AssociationTable(table = @Table(name = "STUDENT_TEACHER"), joinColumns = {@JoinColumn(name = "TEACHER_ID")},inverseJoinColumns = {@JoinColumn(name = "STUDENT_ID")}) public Set { return teachers; } } Dossier.java package com.kuaff.ejb3.relationships; import javax.ejb.Entity; import javax.ejb.GeneratorType; import javax.ejb.Id; @Entity public class Dossier implements java.io.Serializable { private Long id; private String resume; @Id(generate = GeneratorType.AUTO) public Long getId() { return id; } public void setId(Long id) { this.id = id; } public void setResume(String resume) { this.resume = resume; } public String getResume() { return resume; } } |
package com.kuaff.ejb3.relationships; import javax.ejb.AssociationTable; import javax.ejb.Basic; import javax.ejb.CascadeType; import javax.ejb.Column; import javax.ejb.Entity; import javax.ejb.FetchType; import javax.ejb.Id; import javax.ejb.JoinColumn; import javax.ejb.ManyToMany; import javax.ejb.Table; import javax.ejb.Transient; import javax.ejb.Version; import java.util.Set; import javax.ejb.GeneratorType; @Entity public class Teacher implements java.io.Serializable { private Long id; private String resume; private String name; private String info; private Set @Id(generate = GeneratorType.IDENTITY) public Long getId() { return id; } public void setId(Long id) { this.id = id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setInfo(String info) { this.info = info; } public String getInfo() { return info; } public void setStudents(Set { this.students = students; } @ManyToMany(cascade = {CascadeType.CREATE, CascadeType.MERGE}, fetch = FetchType.EAGER) @AssociationTable(table = @Table(name = "STUDENT_TEACHER"), joinColumns = {@JoinColumn(name = "TEACHER_ID",referencedColumnName="ID")}, inverseJoinColumns = {@JoinColumn(name = "STUDENT_ID",referencedColumnName="ID")}) public Set { return students; } } |
package com.kuaff.ejb3.relationships; import javax.ejb.Remote; import java.util.List; @Remote public interface EntityTest { public void createData(); public List findByName(String name); } |
package com.kuaff.ejb3.relationships; import javax.ejb.EntityManager; import javax.ejb.Inject; import javax.ejb.Stateless; import java.util.HashSet; import java.util.Set; import java.util.List; @Stateless public class EntityTestBean implements EntityTest { private @Inject EntityManager manager; public void createData() { Teacher teacher1 = new Teacher(); Teacher teacher2 = new Teacher(); Set Set Student student1 = new Student(); Student student2 = new Student(); Student student3 = new Student(); Dossier dossier1 = new Dossier(); Dossier dossier2 = new Dossier(); Dossier dossier3 = new Dossier(); teacher1.setId(new Long(1)); teacher1.setName("hushisheng"); teacher1.setInfo("胡时胜教授,博士生导师"); manager.create(teacher1); teacher2.setId(new Long(2)); teacher2.setName("liyongchi"); teacher2.setInfo("李永池教授,博士生导师"); manager.create(teacher2); student1.setFirst("晁"); student1.setLast("岳攀"); dossier1.setResume("这是晁岳攀的档案"); student1.setDossier(dossier1); students1.add(student1); student2.setFirst("赵"); student2.setLast("志伟"); dossier2.setResume("这是赵志伟的档案"); student2.setDossier(dossier2); students1.add(student2); student3.setFirst("田"); student3.setLast("明"); dossier3.setResume("这是田明的档案"); student3.setDossier(dossier3); students2.add(student3); teacher1.setStudents(students1); teacher2.setStudents(students2); } public List findByName(String name) { return manager.createQuery("from Teacher t where t.name = :name").setParameter("name", name).listResults(); } } |
package com.kuaff.ejb3.secondary; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.List; public class Client { public static void main(String[] args) throws NamingException { InitialContext ctx = new InitialContext(); StudentDAO dao = (StudentDAO) ctx.lookup(StudentDAO.class.getName()); int id = dao.create("晁","岳攀","8","smallnest@kuaff.com","男"); dao.create("朱","立焕","6","zhuzhu@kuaff.com","女"); List list = dao.findAll(); for(Object o:list) { Student s = (Student)o; System.out.printf("%s%s的性别:%s%n",s.getName().getFirst(),s.getName().getLast(),s.getGender()); dao.evict(s); } } } |