无需XML的hibernate

前端之家收集整理的这篇文章主要介绍了无需XML的hibernate前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



/**
 * 
 */
package com.laotou99.test1;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.laotou99.domain.Test1;
import com.laotou99.domain.Test2;

/**
 * @author gjj
 *
 */
public class MainTest1 {

	/**
	 * 
	 */
	public MainTest1() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) { 
		
		Configuration conf = new Configuration()
			.addAnnotatedClass(com.laotou99.domain.Test1.class)
			.addAnnotatedClass(com.laotou99.domain.Test2.class)
			.setProperty("hibernate.connection.driver_class","com.MysqL.jdbc.Driver")
			.setProperty("hibernate.connection.url","jdbc:MysqL://localhost:3306/abc2?"
						+ "useUnicode=true&"
						+ "connectionCollation=utf8_general_ci&"
						+ "characterEncoding=UTF-8")
			.setProperty("hibernate.connection.username","root")
			.setProperty("hibernate.connection.password","")
			.setProperty("hibernate.c3p0.max_size","20")
			.setProperty("hibernate.c3p0.min_size","1")
			.setProperty("hibernate.c3p0.timeout","5000")
			.setProperty("hibernate.c3p0.max_statements","100")
			.setProperty("hibernate.c3p0.idle_test_period","3000")
			.setProperty("hibernate.c3p0.acquire_increment","2")
			.setProperty("hibernate.c3p0.validate","true")
			.setProperty("hibernate.dialect","org.hibernate.dialect.MysqL5InnoDBDialect")
			.setProperty("hibernate.hbm2ddl.auto","update");
				
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
			.applySettings(conf.getProperties()).build();
		SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
		Session sess = sf.openSession();
		Transaction tx = sess.beginTransaction();
				

		Test1 t1 = new Test1();
		Test2 t2 = new Test2();
		
		t2.setField1("aabb1");
		t2.setField2("aabb2");
		t2.setField3("aabb3");
		t2.setField4("aabb4");
		sess.persist(t2);
		
		t1.setField1("aaa");
		t1.setField2("aaa");
		t1.setField3("aaa");
		t1.setField4("aaa");
		t1.setField5("aaa");
		t1.setField6("aaa");
		t1.setField7("aaa");
		t1.getTest2().add(t2);
		
		sess.save(t1);
		
		Test2 t21 = new Test2();
		
		t21.setField1("aa2bb");
		t21.setField2("aa2bb");

		sess.persist(t21);

		t1.getTest2().add(t21);

//		sess.save(t1);
		tx.commit();
		sess.close();
	}

}

package com.laotou99.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="Test1")
public class Test1 {

	@Id
	@Column(name="test1_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private long id;
	
	private String field1;
	private String field2;
	private String field3;
	private String field4;
	private String field5;
	private String field6;
	private String field7;
	
	@OneToMany(targetEntity=Test2.class)
	@JoinColumn(name="test1_id",referencedColumnName="test1_id")
	private Set<Test2> Test2 = new HashSet<Test2>();
	
	public Set<Test2> getTest2() {
		return Test2;
	}
	public void setTest2(Set<Test2> Test2) {
		this.Test2 = Test2;
	}
	public String getField1() {
		return field1;
	}
	public void setField1(String field1) {
		this.field1 = field1;
	}
	public String getField2() {
		return field2;
	}
	public void setField2(String field2) {
		this.field2 = field2;
	}
	public String getField3() {
		return field3;
	}
	public void setField3(String field3) {
		this.field3 = field3;
	}
	public String getField4() {
		return field4;
	}
	public void setField4(String field4) {
		this.field4 = field4;
	}
	public String getField5() {
		return field5;
	}
	public void setField5(String field5) {
		this.field5 = field5;
	}
	public String getField6() {
		return field6;
	}
	public void setField6(String field6) {
		this.field6 = field6;
	}
	public String getField7() {
		return field7;
	}
	public void setField7(String field7) {
		this.field7 = field7;
	}
	
	
}

package com.laotou99.domain;

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

@Entity
@Table(name="Test2")
public class Test2 {

	@Id
	@Column(name="test2_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private long id;
	
	private String field1;
	private String field2;
	private String field3;
	private String field4;

	public String getField1() {
		return field1;
	}

	public void setField1(String field1) {
		this.field1 = field1;
	}

	public String getField2() {
		return field2;
	}

	public void setField2(String field2) {
		this.field2 = field2;
	}

	public String getField3() {
		return field3;
	}

	public void setField3(String field3) {
		this.field3 = field3;
	}

	public String getField4() {
		return field4;
	}

	public void setField4(String field4) {
		this.field4 = field4;
	}

	
	
}

猜你在找的XML相关文章