EJB。
下面针对每个文件的内容做一个介绍。
Student.java
package com.kuaff.ejb3.entity;
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.OneToMany;
import javax.ejb.Table;
import java.util.ArrayList;
import java.util.Collection;
import java.io.Serializable;
@Entity
@Table(name = "STUDENT")
public class Student implements Serializable
{
//主键
private int id;
//学生名
private String name;
//学生的分数
private Collection<Score> scores;
//主键自动产生
@Id(generate = GeneratorType.AUTO)
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void addScores(String name,int number)
{
if (scores == null)
{
scores = new ArrayList<Score>();
}
Score score = new Score();
score.setName(name);
score.setNumber(number);
score.setStudent(this);
scores.add(score);
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "student_id")
public Collection<Score> getScores()
{
return scores;
}
public void setScores(Collection<Score> scores)
{
this.scores = scores;
}
}
EJB 3.0开发指南之使用实体Bean(3)
时间:2009-11-27 天极
Student.java实现了Student实体Bean,它提供学生的基本情况以及学生的得分情况,得分是另外一个实体Bean。Student实体Bean和Score实体Bean是一对多的关系,站在Score的角度看是多对一的关系。
实体Bean需要使用@Entity做注释,另外它指定这个实体Bean与表STUDENT对应(通过注释@Table(name = "STUDENT")),你可以在JBOSS的数据库中看到这个表。
Score.java
package com.kuaff.ejb3.entity;
import java.io.Serializable;
import javax.ejb.Entity;
import javax.ejb.GeneratorType;
import javax.ejb.Id;
import javax.ejb.JoinColumn;
import javax.ejb.ManyToOne;
import javax.ejb.Table;
@Entity
@Table(name = "Score")
public class Score implements Serializable
{
private int id;
private String name;
private int number;
private Student student;
//主键自动产生
@Id(generate = GeneratorType.AUTO)
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
@ManyToOne
@JoinColumn(name = "student_id")
public Student getStudent()
{
return student;
}
public void setStudent(Student student)
{
this.student = student;
}
}
这个实体Bean存放学生的分数。
Teacher.java
package com.kuaff.ejb3.entity;
import javax.ejb.Remote;
import javax.ejb.Remove;
import java.util.Map;
@Remote
public interface Teacher
{
public void addScore(String studentName,Map<String,Integer> map);
public Student getStudent();
@Remove
public void leave();
}
EJB 3.0开发指南之使用实体Bean(4)
时间:2009-11-27 天极
这个会话Bean接口提供增加分数和得到用 |