Hibernate注解-類級別注解
來源:程序員人生 發布時間:2016-07-07 19:01:29 閱讀次數:3011次
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);
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈