Hibernate-Annotation初步

使用annotation的作用

使用了annotation后可以自动不用写*.hbm.xml的配置文件。方便省事。

 

使用annotation的相关jar文件

在使用annotation时要用到的jar文件:

 

相关文件

对应的jar文件

hibernate-annotations-3.4.0.GA.zip

hibernate-annotations.jar、lib\ejb3-persistence.jar、lib\hibernate-commons-annotations.jar、lib\slf4j-api.jar

hibernate-distribution-3.3.2.GA-dist.zip

hibernate3.jar、lib\required\*.jar

数据库连接

mysql-connector-java-5.1.10-bin.jar

 

 

使用annotation的一个简单实例

要让一个对象(类)使用annotation应该在一个类的头部加上:@Entity,如

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Table;

//加了Entity才会使用到annotation,table意为创建的表名为:t_student

@Entity

@Table(name="t_student")

public class Student {

private Integer id;

private String name;

    //让该字段为id,即设置其为主键,自动增加

    @Id

@GeneratedValue(strategy=GenerationType.AUTO)

    //修改字段的长度为5

@Column(length=5)

public Integer getId() {

return id;

}

    //设置字段名为name,修改字段的长度为20

@Column(name="name",length=20)

public String getName() {

return name;

}

public void setId(Integer id) {

this.id = id;

}

    public void setName(String name) {

this.name = name;

}

}

 

 

以上便写好了一个简单的对象,将会创建一张名为:t_student的数据表,有一个主键id和一个name这样两个字段。要使这样一个对象能自动的生成一张数据表还应该在src目录下的hibernate.cfg.xml配置文件中加入:

 

<mapping class="org.zsl.exam.model.Student"/><!-- 包名加对象名 -->

Hibernate.cfg.xml配置文件

Hibernate.cfg.xml配置文件内容如下:

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

<session-factory>

 

<!-- Database connection settings -->

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.url">jdbc:mysql://localhost:3306/exam</property>

<property name="connection.username">exam</property>

<property name="connection.password">exam123</property>

<property name="connection.useUnicode">true</property>

<property name="connection.characterEncoding">UTF-8</property>

 

 

<!-- 数据库方言,这里为MySQL的数据库 -->

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

 

<!-- Disable the second-level cache  -->

<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

 

<!-- 是否显示sql语句 -->

<property name="show_sql">true</property>

<!-- 自动更新数据库 -->

<property name="hbm2ddl.auto">update</property>

<mapping class="org.zsl.exam.model.Student"/>

</session-factory>

</hibernate-configuration>

Hibernate annotation的一对多

在多的那方加入:

…………

private Teacher teacher;

//说明是多对一

@ManyToOne

//在本表里加入外键的字段名为:tea_id

@JoinColumn(name="tea_id")

public Teacher getTeacher() {

return teacher;

}

    …………

 

在一的那方加入:

    …………

private Set<Student> students;

//说明是一对多,mappedBy指的是在多方对象里指明外键的属性名

//targetEntity表示多方对象

    @OneToMany(mappedBy="teacher",targetEntity=Student.class)

public Set<Student> getStudents() {

return students;

}

    …………

 

Annotation的继承

要使一个对象的annotation被其它对象所继承很简单,只用在父类的头部加上:

@MappedSuperclass

如:

@MappedSuperclass

public class BaseEntity {

 

private Integer id;

 

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(length=5)

public Integer getId() {

return id;

}

 

public void setId(Integer id) {

this.id = id;

}

}

这样只要继承了BaseEntity的子类都会具有一个主键id

<!--EndFragment-->

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值