Hibernate注解-类级别注解
来源:程序员人生 发布时间:2016-07-07 19:01:29 阅读次数:2819次
Hibenate注解
使用注解的目的:为了简化繁琐的ORM映照文件(*.hbm)的配置
JPA 全程Java Persistence API
JPA注解是JAVAEE的规范和标准
JPA与Hibernate的关系:JPA是标准接口,Hibernate是实现,但是其功能是JPA的超集
Hibernate通过hibernate-annotation、hibernate-entitymanager和hibernate-core3个组件来实现与JPA的关系
1般在实际的开发进程中,优先斟酌使用JPA注解,这样更有益于程序的移植和扩大。
Hibernate注解分类
类级别注解
属性级别注解
映照关系注解
类级别注解
@Entity
映照实体类
@Entity(name="tableName")
name:可选,对应数据库中的1个表。若表名与实体类名相同,则可以省略
注意:使用@Entity时必须指定实体类的主键属性
@Table
@Table(name="",catalog="",schema="")
@Entity配合使用,只能标注在实体的class定义处,表示实体对应的数据库表的信息。
name:可选,映照表的名称,默许表名和实体名称1致,只有在不1致的情况下才需要指定表名。
catalog 可选,表示Catalog名称,默许为Catalog("")
schema - 可选,表示Schema名称,默许为Schema("").
从实现的角度看,各种数据库系统对Catalog和Schema的支持和实现方式千差万别
@Embeddable
@Embeddable表示1个非Entity类可以嵌入到另外一个Entity类中作为属性而存在。
实例:
导入所需的包,以下,
hibernate.cfg.xml中的配置,
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration⑶.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_struts_stumanager</property>
-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mypage</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate_current_session_context_class">thread</property>
<mapping class="com.entity.Students"/>
</session-factory>
</hibernate-configuration>
学生实体类:
package com.entity;
import javax.persistence.Entity;//JPA注解
import javax.persistence.Id;
import javax.persistence.Table;
/*
* 学生实体类
*/
//@Entity//
//@Entity(name="t_students")//如果不添加名字,则默许与实体类名字相同,如果想要自行设置表明,就需要自己进行添加
@Entity
@Table(name="t_students1",schema="mypage")
public class Students {
private String sid; //学号
private String sname;//姓名
private String gender;//性别
private String birthday;//诞生日期
private String major;//专业
private Address add;
public Students(){
}
public Students(String sid, String sname, String gender,
String birthday, String major,Address add) {
// super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.major = major;
this.add = add;
}
@Id
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Address getAdd() {
return add;
}
public void setAdd(Address add) {
this.add = add;
}
}
地址类:
package com.entity;
import javax.persistence.Embeddable;
// 地址类
@Embeddable //表示是1个嵌入类,这个类的对象在另外一个实体类中充当属性
public class Address {
private String postCode;//邮编
private String address;//地址
private String phone;//联系电话
public Address(){
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
测试类:
package com.entity;
import java.util.EnumSet;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class TestStudents {
@Test
public void testShemaExport(){
//创建hibernate配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//生成SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
SchemaExport export = new SchemaExport(config);
export.create(true,true);
}
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠