導入Hibernate

src\chap06app:

Main.java

package chap06app;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;


public class Main{
	
	public static void main( String[] args ){
		
		SessionFactory sf = new AnnotationConfiguration().
							configure().buildSessionFactory();
		Session session = sf.openSession();
		
		Transaction tx = session.beginTransaction();
		try{
			// t_prefにTokyoとChibaがすでに入っているものとする。
			// Tokyoを見つける
			List<Pref> tokyoList = session.createQuery( "from Pref pref where pref.pref like 'Tokyo'" ).list();
			Pref tokyoPref;
			if( tokyoList.size() == 1 ){
				tokyoPref = (Pref)tokyoList.get( 0 );
			}else{
				throw new UnsupportedOperationException( "Tokyo not found!");
			}
			
			System.out.println( "tokyoPref id: " + tokyoPref.getId() );
			System.out.println( "TokyoPref pref: " + tokyoPref.getPref() );
			
			Address okadaAddr = new Address();
			okadaAddr.setName( "Okada" );
			okadaAddr.setPref( tokyoPref );
			okadaAddr.setAddress( "Edogawaku 1-2-3" );
			
			session.save( okadaAddr );
			
			tx.commit();
		}catch( HibernateException he ){
			if( tx != null ){ tx.rollback(); }	
		}finally{
			session.close();
			sf.close();
		}
	}
}

Pref.java

package chap06app;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "t_pref", schema = "public")
public class Pref implements java.io.Serializable {

	private int id;
	private String pref;
	private Set<Address> TAddresses = new HashSet<Address>(0);

	public Pref() {
	}

	public Pref(int id, String pref) {
		this.id = id;
		this.pref = pref;
	}

	public Pref(int id, String pref, Set<Address> TAddresses) {
		this.id = id;
		this.pref = pref;
		this.TAddresses = TAddresses;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "pref", nullable = false)
	public String getPref() {
		return this.pref;
	}

	public void setPref(String pref) {
		this.pref = pref;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "pref")
	public Set<Address> getTAddresses() {
		return this.TAddresses;
	}

	public void setTAddresses(Set<Address> TAddresses) {
		this.TAddresses = TAddresses;
	}

}

Address.java

package chap06app;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "t_address2", schema = "public")
public class Address implements java.io.Serializable {

	private int id;
	private Pref pref;
	private String name;
	private String address;

	public Address() {
	}

	public Address(int id, Pref pref, String name, String address) {
		this.id = id;
		this.pref = pref;
		this.name = name;
		this.address = address;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pref_id_fk", nullable = false)
	public Pref getPref() {
		return this.pref;
	}

	public void setPref(Pref pref) {
		this.pref = pref;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "address", nullable = false)
	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}

src\out:

TAddress.java

// default package
// Generated 2009/01/29 0:32:15 by Hibernate Tools 3.2.1.GA

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * TAddress generated by hbm2java
 */
@Entity
@Table(name = "t_address", schema = "public")
public class TAddress implements java.io.Serializable {

	private int id;
	private String name;
	private int pref;
	private String address;

	public TAddress() {
	}

	public TAddress(int id, String name, int pref, String address) {
		this.id = id;
		this.name = name;
		this.pref = pref;
		this.address = address;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "pref", nullable = false)
	public int getPref() {
		return this.pref;
	}

	public void setPref(int pref) {
		this.pref = pref;
	}

	@Column(name = "address", nullable = false)
	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}

TAddress2.java

// default package
// Generated 2009/01/29 0:32:15 by Hibernate Tools 3.2.1.GA

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * TAddress2 generated by hbm2java
 */
@Entity
@Table(name = "t_address2", schema = "public")
public class TAddress2 implements java.io.Serializable {

	private int id;
	private TPref TPref;
	private String name;
	private String address;

	public TAddress2() {
	}

	public TAddress2(int id, TPref TPref, String name, String address) {
		this.id = id;
		this.TPref = TPref;
		this.name = name;
		this.address = address;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pref_id_fk", nullable = false)
	public TPref getTPref() {
		return this.TPref;
	}

	public void setTPref(TPref TPref) {
		this.TPref = TPref;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "address", nullable = false)
	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}

TPref.java

// default package
// Generated 2009/01/29 0:32:15 by Hibernate Tools 3.2.1.GA

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * TPref generated by hbm2java
 */
@Entity
@Table(name = "t_pref", schema = "public")
public class TPref implements java.io.Serializable {

	private int id;
	private String pref;
	private Set<TAddress2> TAddress2s = new HashSet<TAddress2>(0);

	public TPref() {
	}

	public TPref(int id, String pref) {
		this.id = id;
		this.pref = pref;
	}

	public TPref(int id, String pref, Set<TAddress2> TAddress2s) {
		this.id = id;
		this.pref = pref;
		this.TAddress2s = TAddress2s;
	}

	@Id
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "pref", nullable = false)
	public String getPref() {
		return this.pref;
	}

	public void setPref(String pref) {
		this.pref = pref;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TPref")
	public Set<TAddress2> getTAddress2s() {
		return this.TAddress2s;
	}

	public void setTAddress2s(Set<TAddress2> TAddress2s) {
		this.TAddress2s = TAddress2s;
	}

}

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>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">okada</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Chap04DB</property>
        <property name="hibernate.connection.username">okada</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <mapping class="TAddress2" />
        <mapping class="TAddress" />
        <mapping class="TPref" />
    </session-factory>
</hibernate-configuration>

src:

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>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">okada</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Chap04DB</property>
        <property name="hibernate.connection.username">okada</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.show_sql">true</property>

		<mapping class="chap06app.Address"/>
        <mapping class="chap06app.Pref"/>
    </session-factory>
</hibernate-configuration>

代碼來自日本的技術圖書http://www.shuwasystem.co.jp/products/7980html/2197.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章