<?xml version="1.0"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="hibernate.connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="hibernate.connection.url"> jdbc:oracle:thin:@XX.26.158.43:1521:orcl </property> <property name="hibernate.connection.username"></property> <property name="hibernate.connection.password"></property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <mapping resource="com/oscar999/trans/hibernate/Product.hbm.xml"/> </session-factory> </hibernate-configuration>
/**
* @Title: Product.java
* @Package com.oscar999.trans.hibernate
* @Description:
* @author XM
* @date Feb 15, 2017 1:44:47 PM
* @version V1.0
*/
package com.oscar999.trans.hibernate;
import java.io.Serializable;
/**
* @author XM
*
*/
public class Product implements Serializable {
public Product() {
}
private Integer id;
private String name;
private String price;
private static final long serialVersionUID = 1L;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <class name="com.oscar999.trans.hibernate.Product" table="TEST_PRODUCT"> <id name="id" column="id" type="java.lang.Integer"> <generator class="assigned" /> </id> <property name="name" column="name" type="java.lang.String" not-null="true" unique="true" length="20" /> <property name="price" column="price" type="java.lang.String" not-null="false" unique="false" length="20" /> </class> </hibernate-mapping>
/**
* @Title: TestMain.java
* @Package com.oscar999.trans.hibernate
* @Description:
* @author XM
* @date Feb 15, 2017 2:02:17 PM
* @version V1.0
*/
package com.oscar999.trans.hibernate;
import java.io.File;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
* @author XM
*
*/
public class TestMain {
/**
* @param args
*/
public Session getSession() {
Session session = null;
Configuration conf = new Configuration().configure(new File("src/com/oscar999/trans/hibernate/hibernate.cfg.xml"));
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
session = sf.openSession();
return session;
}
public void addProduct(Session session, String name, String price) {
Transaction t = session.beginTransaction(); // 1. comment1
Product product = new Product();
product.setId(1);
product.setName(name);
product.setPrice(price);
try {
session.save(product);
t.commit(); // 2. comment2
} catch (Exception e) {
t.rollback();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMain testMain = new TestMain();
Session session = testMain.getSession();
testMain.addProduct(session, "product1", "price1");
if (session != null && session.isOpen()) {
session.close();
}
}
}
#hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory #hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory
javax.transaction.UserTransaction tx = new InitialContext();.lookup("javax.transaction.UserTransaction");;
Session s1 = sf.openSession();;
...
s1.flush();;
s1.close();;
...
Session s2 = sf.openSession();;
...
s2.flush();;
s2.close();;
tx.commit();;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有