java

位置:IT落伍者 >> java >> 浏览文章

开发框架 hibernate3.0 开发实例


发布日期:2022年07月16日
 
开发框架 hibernate3.0 开发实例

建议是有一点点是一点点基础的人又没有hibernate基础和经验的人比较适合(所谓一点点基础是最起码不要我介绍一些配置文件的什么的)注意我用的JDBC驱动cominettdsTdsDriver

大家一起努力学习吧

建立PO对象

建PO对象CustomerOrder以及它们的hibernate配置文件

/*

* Created on

*

* TODO To change the template for this generated file go to

* Window Preferences Java Code Style Code Templates

*/

package testdemo;

import javautilHashSet;

import javautilSet;

/**

* @author liuzj

*

* TODO To change the template for this generated type comment go to Window

* Preferences Java Code Style Code Templates

*/

public class Customer {

private int id;

private String username;

private String password;

private Set orders = new HashSet();

public Customer() {

}

public Customer(String username String password Set orders) {

thisusername = username;

thispassword = password;

thisorders = orders;

}

public int getId() {

return id;

}

public String getPassword() {

return password;

}

public String getUsername() {

return username;

}

public Set getOrders() {

return orders;

}

public void setId(int id) {

thisid = id;

}

public void setPassword(String password) {

thispassword = password;

}

public void setUsername(String username) {

thisusername = username;

}

public void setOrders(Set orders) {

thisorders = orders;

}

}

<?xml version=?>

<!DOCTYPE hibernatemapping PUBLIC

//Hibernate/Hibernate Mapping DTD //EN

mappingdtd>

<hibernatemapping>

<class name=testdemoCustomer table=CUSTOMER dynamicinsert=true dynamicupdate=true>

<id name=id column=ID>

<generator class=increment />

</id>

<property name=username column=USERNAME />

<property name=password column=PASSWORD />

<set

name=orders

inverse=true

cascade=saveupdate

>

<key column=CUSTOMER_ID />

<onetomany class=testdemoOrder />

</set>

</class>

</hibernatemapping>

/*

* Created on

*

* TODO To change the template for this generated file go to

* Window Preferences Java Code Style Code Templates

*/

package testdemo;

/**

* @author liuzj

*

* TODO To change the template for this generated type comment go to

* Window Preferences Java Code Style Code Templates

*/

import javaioSerializable;

public class Order implements Serializable {

private Long id;

private String orderNumber;

private double price;

private Customer customer;

public Order() {

}

public Order(String orderNumberdouble priceCustomer customer) {

thisorderNumber=orderNumber;

thisprice=price;

thiscustomer=customer;

}

public Long getId() {

return thisid;

}

public void setId(Long id) {

thisid = id;

}

public String getOrderNumber() {

return thisorderNumber;

}

public void setOrderNumber(String orderNumber) {

thisorderNumber = orderNumber;

}

public Customer getCustomer() {

return thiscustomer;

}

public void setCustomer(Customer customer) {

thiscustomer = customer;

}

public double getPrice(){

return thisprice;

}

private void setPrice( double price ){

thisprice = price;

}

}

<?xml version=?>

<!DOCTYPE hibernatemapping PUBLIC

//Hibernate/Hibernate Mapping DTD //EN

mappingdtd>

<hibernatemappingpackage=testdemo>

<class name=Ordertable=ORDERS>

<id name=id>

<generator class=increment/>

</id>

<property name=orderNumber column=ORDER_NUMBER/>

<property name=price />

<manytoone

name=customer

column=CUSTOMER_ID

class=Customer

notnull=true

/>

</class>

</hibernatemapping>

上面的PO已经建立完成下面是一个测试类

/*

* Created on

*

* TODO To change the template for this generated file go to

* Window Preferences Java Code Style Code Templates

*/

package testdemo;

import javautilHashSet;

import orghibernateSession;

import orghibernateSessionFactory;

import orghibernateTransaction;

import orghibernatecfgConfiguration;

import testdemoCustomer;

/**

* @author liuzj

*

* TODO To change the template for this generated type comment go to Window

* Preferences Java Code Style Code Templates

*/

public class Test {

SessionFactory sessionFactory = new Configuration(nfigure()addClass(

testdemoCustomerclass)addClass(Orderclass)buildSessionFactory();

Session session = sessionFactoryopenSession();

public void saveCustomer(Customer customer) throws Exception {

Transaction tx = null;

try {

tx = sessionbeginTransaction();

sessionsave(customer);

mit();

} catch (Exception e) {

if (tx != null) {

txrollback();

}

throw e;

} finally {

sessionclose();

}

}

public void testmethod()throws Exception

{

Customer customer=new Customer(lzhengjnew HashSet());

Order order=new Order(Ordercustomer);

Order order=new Order(Ordercustomer);

customergetOrders()add(order);

customergetOrders()add(order);

thissaveCustomer(customer);

}

public static void main(String[] args) {

try{

new Test()testmethod();

}catch(Exception e)

{

Systemoutprintln(this is the testmethodthrow exception);

eprintStackTrace();

}

}

}

ok下面是一个hibernate的配置hibernatecfgxml(位于应用目录下面)

<!DOCTYPE hibernateconfiguration PUBLIC

//Hibernate/Hibernate Configuration DTD //EN

configurationdtd>

<hibernateconfiguration>

<sessionfactory>

<property name=nnectionurl>

jdbc:inetdae:localhost:?charset=gbk&database=hibernate_test

</property>

<property name=nnectiondriver_class>

cominettdsTdsDriver

</property>

<property name=nnectionusername>

sa

</property>

<property name=nnectionpassword>

aa

</property>

<property name=hibernatedialect>

orghibernatedialectSQLServerDialect

</property>

<property name=show_sql>

true

&               

上一篇:Hibernate优化

下一篇:Java GUI三剑客风云争霸