Hibernate的Anotation使用,为Spring的Anotation打下基础,多对多数据映射
2014-03-15 11:47
405 查看
Hibernate的Anotation使用,为Spring的Anotation打下基础,多对多数据映射
首先建表:主要是中间表
CREATE TABLE SCORE(
SNO VARCHAR2(40),
CNO NUMBER(5),
SCORENUMBER(10),
PRIMARY KEY(SNO,CNO),
FOREIGN KEY (SNO) REFERENCES STUDENT2(SNO) ON DELETECASCADE,
FOREIGN KEY (CNO) REFERENCES COURSE(CNO) ON DELETECASCADE
)
选中后,在HibernateSessionFactory中,就会改变Configuration的类型。
private static Configuration configuration = new AnnotationConfiguration();
生成映射时,需要选择使用Annotation的方式生成,其他与之前没有区别。
自动pojo类,不用xml配置,类中自己配置
package com.kane.pojo;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* Score entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "SCORE", schema = "ZL")
public class Score implements java.io.Serializable {
// Fields
private ScoreId id;
private Course course;
private Student2 student2;
private Integer score;
// Constructors
/** default constructor */
public Score() {
}
/** minimal constructor */
public Score(ScoreId id, Coursecourse, Student2 student2) {
this.id = id;
this.course = course;
this.student2 =student2;
}
/** full constructor */
public Score(ScoreId id, Coursecourse, Student2 student2, Integer score) {
this.id = id;
this.course = course;
this.student2 =student2;
this.score = score;
}
// Property accessors
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name= "sno", column = @Column(name = "SNO", nullable = false,length = 40)),
@AttributeOverride(name= "cno", column = @Column(name = "CNO", nullable = false,precision = 5, scale = 0)) })
public ScoreId getId() {
return this.id;
}
public void setId(ScoreId id) {
this.id = id;
}
@ManyToOne(fetch =FetchType.LAZY)
@JoinColumn(name ="CNO", nullable = false, insertable = false, updatable = false)
public Course getCourse() {
return this.course;
}
public void setCourse(Coursecourse) {
this.course = course;
}
@ManyToOne(fetch =FetchType.LAZY)
@JoinColumn(name ="SNO", nullable = false, insertable = false, updatable = false)
public Student2 getStudent2() {
return this.student2;
}
public void setStudent2(Student2student2) {
this.student2 =student2;
}
@Column(name = "SCORE",precision = 10, scale = 0)
public Integer getScore() {
return this.score;
}
public void setScore(Integerscore) {
this.score = score;
}
}
package com.kane.pojo;
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.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
/**
* Course entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "COURSE", schema = "ZL")
public class Course implements java.io.Serializable {
// Fields
private Integer cno;
private String cname;
private Set<Score> scores =new HashSet<Score>(0);
// Constructors
/** default constructor */
public Course() {
}
/** full constructor */
public Course(String cname,Set<Score> scores) {
this.cname = cname;
this.scores = scores;
}
// Property accessors
@GenericGenerator(name ="generator", strategy = "increament")
@Id
@GeneratedValue(generator ="generator")
@Column(name = "CNO",unique = true, nullable = false, precision = 5, scale = 0)
public Integer getCno() {
return this.cno;
}
public void setCno(Integer cno) {
this.cno = cno;
}
@Column(name = "CNAME",length = 100)
public String getCname() {
return this.cname;
}
public void setCname(Stringcname) {
this.cname = cname;
}
// mappedBy等同于XML里的inverse="true",表示关联关系由对方对象
中的course属性控制。
// 一定要删除cascade配置,否则在进行课程的添加和修改时,student也会跟着一起操作.
@OneToMany( fetch =FetchType.LAZY, mappedBy = "course")//应该是学生进行维护
public Set<Score>getScores() {
return this.scores;
}
public void setScores(Set<Score>scores) {
this.scores = scores;
}
}
功能:当学生登陆后,列出所有课程供用户选择。登陆同时,注意,需要将之前选择的课程内容取得,并保存到student里。
package com.kane.dao.Impl;
import java.util.List;
import com.kane.dao.IStudent2;
import com.kane.dbc.HibernateSessionFactory;
import com.kane.pojo.Student2;
public class Student2Impl implements IStudent2{
public void docreate(Student2 v)throws Exception {
// TODO Auto-generatedmethod stub
}
public void doremove(String id)throws Exception {
// TODO Auto-generatedmethod stub
}
public List<Student2>findAll() throws Exception {
// TODO Auto-generatedmethod stub
return null;
}
public Student2 findById(Stringid) throws Exception {
//这里返回的对象要赋值给action,里面可以根据外键取出课程信息
return(Student2)HibernateSessionFactory.getSession().get(Student2.class,id);
}
public List<Student2>findByPage(int pageNo, int pageSize, String column,
Stringkeyword) throws Exception {
// TODO Auto-generatedmethod stub
return null;
}
public int getCount(String column,String keyword) throws Exception {
// TODO Auto-generatedmethod stub
return 0;
}
public void update(Student2 v)throws Exception {
// TODO Auto-generatedmethod stub
}
}
首先建表:主要是中间表
CREATE TABLE SCORE(
SNO VARCHAR2(40),
CNO NUMBER(5),
SCORENUMBER(10),
PRIMARY KEY(SNO,CNO),
FOREIGN KEY (SNO) REFERENCES STUDENT2(SNO) ON DELETECASCADE,
FOREIGN KEY (CNO) REFERENCES COURSE(CNO) ON DELETECASCADE
)
选中后,在HibernateSessionFactory中,就会改变Configuration的类型。
private static Configuration configuration = new AnnotationConfiguration();
生成映射时,需要选择使用Annotation的方式生成,其他与之前没有区别。
自动pojo类,不用xml配置,类中自己配置
package com.kane.pojo;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* Score entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "SCORE", schema = "ZL")
public class Score implements java.io.Serializable {
// Fields
private ScoreId id;
private Course course;
private Student2 student2;
private Integer score;
// Constructors
/** default constructor */
public Score() {
}
/** minimal constructor */
public Score(ScoreId id, Coursecourse, Student2 student2) {
this.id = id;
this.course = course;
this.student2 =student2;
}
/** full constructor */
public Score(ScoreId id, Coursecourse, Student2 student2, Integer score) {
this.id = id;
this.course = course;
this.student2 =student2;
this.score = score;
}
// Property accessors
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name= "sno", column = @Column(name = "SNO", nullable = false,length = 40)),
@AttributeOverride(name= "cno", column = @Column(name = "CNO", nullable = false,precision = 5, scale = 0)) })
public ScoreId getId() {
return this.id;
}
public void setId(ScoreId id) {
this.id = id;
}
@ManyToOne(fetch =FetchType.LAZY)
@JoinColumn(name ="CNO", nullable = false, insertable = false, updatable = false)
public Course getCourse() {
return this.course;
}
public void setCourse(Coursecourse) {
this.course = course;
}
@ManyToOne(fetch =FetchType.LAZY)
@JoinColumn(name ="SNO", nullable = false, insertable = false, updatable = false)
public Student2 getStudent2() {
return this.student2;
}
public void setStudent2(Student2student2) {
this.student2 =student2;
}
@Column(name = "SCORE",precision = 10, scale = 0)
public Integer getScore() {
return this.score;
}
public void setScore(Integerscore) {
this.score = score;
}
}
package com.kane.pojo;
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.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
/**
* Course entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "COURSE", schema = "ZL")
public class Course implements java.io.Serializable {
// Fields
private Integer cno;
private String cname;
private Set<Score> scores =new HashSet<Score>(0);
// Constructors
/** default constructor */
public Course() {
}
/** full constructor */
public Course(String cname,Set<Score> scores) {
this.cname = cname;
this.scores = scores;
}
// Property accessors
@GenericGenerator(name ="generator", strategy = "increament")
@Id
@GeneratedValue(generator ="generator")
@Column(name = "CNO",unique = true, nullable = false, precision = 5, scale = 0)
public Integer getCno() {
return this.cno;
}
public void setCno(Integer cno) {
this.cno = cno;
}
@Column(name = "CNAME",length = 100)
public String getCname() {
return this.cname;
}
public void setCname(Stringcname) {
this.cname = cname;
}
// mappedBy等同于XML里的inverse="true",表示关联关系由对方对象
中的course属性控制。
// 一定要删除cascade配置,否则在进行课程的添加和修改时,student也会跟着一起操作.
@OneToMany( fetch =FetchType.LAZY, mappedBy = "course")//应该是学生进行维护
public Set<Score>getScores() {
return this.scores;
}
public void setScores(Set<Score>scores) {
this.scores = scores;
}
}
功能:当学生登陆后,列出所有课程供用户选择。登陆同时,注意,需要将之前选择的课程内容取得,并保存到student里。
package com.kane.dao.Impl;
import java.util.List;
import com.kane.dao.IStudent2;
import com.kane.dbc.HibernateSessionFactory;
import com.kane.pojo.Student2;
public class Student2Impl implements IStudent2{
public void docreate(Student2 v)throws Exception {
// TODO Auto-generatedmethod stub
}
public void doremove(String id)throws Exception {
// TODO Auto-generatedmethod stub
}
public List<Student2>findAll() throws Exception {
// TODO Auto-generatedmethod stub
return null;
}
public Student2 findById(Stringid) throws Exception {
//这里返回的对象要赋值给action,里面可以根据外键取出课程信息
return(Student2)HibernateSessionFactory.getSession().get(Student2.class,id);
}
public List<Student2>findByPage(int pageNo, int pageSize, String column,
Stringkeyword) throws Exception {
// TODO Auto-generatedmethod stub
return null;
}
public int getCount(String column,String keyword) throws Exception {
// TODO Auto-generatedmethod stub
return 0;
}
public void update(Student2 v)throws Exception {
// TODO Auto-generatedmethod stub
}
}
相关文章推荐
- Hibernate数据的lazy问题,以及使用spring整合hibernate lazy的解决办法(好文转载)
- 使用spring、springmvc和hibernate开发博客(二)——数据进出
- struts+spring+hibernate架构中数据对象(PO,POJO,FormBean)的使用心得
- 文件映射spring 使用classpath方式加载hibernate映射文件
- 使用Hibernate 创建数据表时,如何使数据库中的字段的长度和Hibernate映射的长度相等呢?
- spring(11)使用对象-关系映射持久化数据
- Spring学习笔记之使用对象-关系映射持久化数据
- 使用Spring+Hibernate实现Oracle数据库BlOB数据类型的读写
- 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能
- 使用Hibernate映射技术将Mysql数据复制到Oracle数据库
- spring 使用classpath方式加载hibernate映射文件
- Spring使用通配符自动加载hibernate映射文件生成sessionFactory
- flex tree 怎么在java后台组合成合适的数据,我使用的是flex spring hibernate
- hibernate对象映射Date数据类型和input datetime使用注意事项
- 使用springmvc+jackson+hibernate返回JSON数据
- 使用Spring的mappingDirectoryLocations来配置Hibernate映射文件
- 使用Spring+Hibernate实现Oracle数据库BlOB数据类型的读写
- 使用Spring的mappingDirectoryLocations来配置Hibernate映射文件
- 使用Spring的mappingDirectoryLocations来配置Hibernate映射文件
- 使用Spring 来配置 hibernate 映射文件的 两种方法