您的位置:首页 > 编程语言 > Java开发

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

                  

         }

}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: