`
henghengdh
  • 浏览: 153412 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hibernate别名Alias

阅读更多
实体:考试,成绩,学生,班级
关系:成绩与考试多对一,成绩与学生多对一,学生与班级多对一.
需求:要根据考试id和班级id查询学生成绩.

Score
@Entity  
public class Score {  
  
    private int id;  
  
    private Student student;  
  
    private Exam exam;  
  
    private int score;  
  
    @Id  
    @GeneratedValue(strategy = GenerationType.IDENTITY)  
    public int getId() {  
        return id;  
    }  
  
    public void setId(int id) {  
        this.id = id;  
    }  
  
    @ManyToOne  
    @JoinColumn(name = "studentid", referencedColumnName = "id")  
    public Student getStudent() {  
        return student;  
    }  
  
    public void setStudent(Student student) {  
        this.student = student;  
    }  
  
    @ManyToOne  
    @JoinColumn(name = "examid", referencedColumnName = "id")  
    public Exam getExam() {  
        return exam;  
    }  
  
    public void setExam(Exam exam) {  
        this.exam = exam;  
    }  
  
    public int getScore() {  
        return score;  
    }  
  
    public void setScore(int score) {  
        this.score = score;  
    }  
  
}  


Student
private int id;  
  
    private Grade grade;  
  
    private String name;  
    @Id  
    @GeneratedValue(strategy = GenerationType.IDENTITY)  
    public int getId() {  
        return id;  
    }  
  
    public void setId(int id) {  
        this.id = id;  
    }  
  
    @ManyToOne  
    @JoinColumn(name = "gradeid", referencedColumnName = "id")  
    public Grade getGrade() {  
        return grade;  
    }  
  
    public void setGrade(Grade grade) {  
        this.grade = grade;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  


Exam和Grade略

下面是查询代码.
ScoreDao:
@SuppressWarnings("unchecked")  
    public List<Score> getList(int examid, int gradeid) {  
        Criteria c = getSession().createCriteria(Score.class);  
        c.setFetchMode("exam", FetchMode.JOIN);  
        c.add(Restrictions.eq("exam.id", examid));  
        c.createAlias("student.grade", "grade");  
        c.add(Restrictions.eq("grade.id", gradeid));  
  
        return c.list();  
    }  


转自:http://www.verydemo.com/demo_c146_i2502.html
分享到:
评论

相关推荐

    Hibernate+中文文档

    16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support) 20.1. Summary 20.2. SchemaExport...

    hibernate3.2中文文档(chm格式)

    16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support) 20.1. Summary 20.2. SchemaExport...

    HibernateAPI中文版.chm

    16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support) 20.1. Summary 20.2. SchemaExport...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support) 20.1. Summary 20.2. SchemaExport...

    Hibernate 中文 html 帮助文档

    别名和属性引用(Alias and property references) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用...

    Hibernate参考文档

    别名和属性引用(Alias and property references) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用...

    spring.doc

    3.2别名Alias 11 别名拓展: 11 3.3 Spring容器内部对象的创建 12 Spring容器内部对象创建拓展: 12 3.3.1使用类构造器实例化(默认无参数) 14 3.3.2使用静态工厂方法实例化(简单工厂模式) 14 3.3.3初始化(创建)bean...

    ibatis 开发指南(pdf)

    使用ibatis 提供的ORM 机制,对业务逻辑实现人员而言,面对的是纯粹的Java 对象, 这一层与通过Hibernate 实现ORM 而言基本一致,而对于具体的数据操作,Hibernate 会自动生成SQL 语句,而ibatis 则要求...

    NHibernate参考文档 2.0.0 chm

    别名和属性引用(Alias and property references) 14.1.5. 返回非受管实体(Returning non-managed entities) 14.1.6. 处理继承(Handling inheritance) 14.1.7. 参数(Parameters) 14.2. 命名SQL查询 14.2.1. 使用...

    springmybatis

    1.Configuration.xml 是 mybatis 用来建立 sessionFactory 用的,里面主要包含了数据库连接相关东西,还有 java 类所对应的别名,比如 &lt;typeAlias alias="User" type="com.yihaomen.mybatis.model.User"/&gt; 这个别名...

    NHibernate中文帮组文档(2008.11月更新)

    别名和属性引用(Alias and property references) 14.1.5. 返回非受管实体(Returning non-managed entities) 14.1.6. 处理继承(Handling inheritance) 14.1.7. 参数(Parameters) 14.2. 命名SQL查询 14.2.1. 使用...

Global site tag (gtag.js) - Google Analytics