標籤:hibernate工具類 多對一關聯性 註解 hibernate
實體類1
package entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Dream{
private int did;
private String ddesc;
private Person person;
@ManyToOne
@JoinColumn
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public String getDdesc() {
return ddesc;
}
public void setDdesc(String ddesc) {
this.ddesc = ddesc;
}
}
實體類2
package entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person{
private int pid;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
設定檔:
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- SQL dialect 資料庫方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC connection pool (use the built-in)串連池 -->
<property name="connection.pool_size">2</property>
<!-- Enable Hibernate‘s current session context 當前session-->
<property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout 在控制台顯示sql語句-->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup
java代碼自動產生資料庫裡面的表
create 每次都會根據java代碼,建立表,刪除上次的表
update 【常用】 在上一次的基礎上修改資訊
create——drop 根據類產生表
validate 不建立表,但是會插入新值 -->
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<!-- 將實體類中的設定檔引入hibernate中,聲明設定檔 -->
<mapping class="entity.Person"></mapping>
<mapping class="entity.Dream"></mapping>
</session-factory>
</hibernate-configuration>
測試類別:
package test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;
import entity.Dream;
import entity.Person;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Person p=new Person();
p.setName("韓迎賓");
Dream d=new Dream();
d.setDdesc("5年後寫一個Oracle");
d.setPerson(p);
Session session=HibernateUtil.getCurrentSession();
Transaction transaction=session.beginTransaction();
session.save(p);
session.save(d);
transaction.commit();
}
}
Hibernate註解多對一關聯關係