Hibernate的Anotation使用,为Spring的Anotation打下基础,多对多数据映射

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
                  
         }


}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值