Hibernate注解-类级别注解

来源:互联网 发布:大麦户源码程序 编辑:程序博客网 时间:2024/04/20 08:12

Hibenate注解

     使用注解的目的:为了简化繁琐的ORM映射文件(*.hbm)的配置
     JPA 全程Java Persistence API
     JPA注解是JAVAEE的规范和标准
     JPA与Hibernate的关系:JPA是标准接口,Hibernate是实现,但是其功能是JPA的超集
     Hibernate通过hibernate-annotation、hibernate-entitymanager和hibernate-core三个组件来实现与JPA的关系

     一般在实际的开发过程中,优先考虑使用JPA注解,这样更有利于程序的移植和扩展。

Hibernate注解分类

      类级别注解

      属性级别注解

      映射关系注解

类级别注解

      @Entity

           映射实体类
  @Entity(name="tableName")
  name:可选,对应数据库中的一个表。若表名与实体类名相同,则可以省略
  注意:使用@Entity时必须指定实体类的主键属性

     @Table

                    @Table(name="",catalog="",schema="")
  @Entity配合使用,只能标注在实体的class定义处,表示实体对应的数据库表的信息。
  name:可选,映射表的名称,默认表名和实体名称一致,只有在不一致的情况下才需要指定表名。
  catalog 可选,表示Catalog名称,默认为Catalog("")
  schema - 可选,表示Schema名称,默认为Schema("").
  从实现的角度看,各种数据库系统对Catalog和Schema的支持和实现方式千差万别

       @Embeddable

                    @Embeddable表示一个非Entity类可以嵌入到另一个Entity类中作为属性而存在。
       实例:
           导入所需的包,如下,
                
         hibernate.cfg.xml中的配置,
                
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.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;}        @Idpublic 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 //表示是一个嵌入类,这个类的对象在另一个实体类中充当属性 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);       }}

       
1 0