我正在使用hibernate将我的对象添加到数据库中。
当我调用sessionCacti.save(Object object)
时,它将对象存储到数据库,而对象不应该存储,因为我没有提交更改,而是使用非自动提交模式。
这是hibernate配置的文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.current_session_context_class">thread</property>
<!-- pcac is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://uaeipvm1:3307/pcac?zeroDateTimeBehavior=convertToNull
</property>
<property name="hibernate.connection.username">
myadmin
</property>
<property name="hibernate.connection.password">
dbaft
</property>
<!-- 1: READ UNCOMMITTED 2: READ COMMITTED 4: REPEATABLE READ 8: SERIALIZABLE -->
<property name="hibernate.connection.isolation">2</property>
<!-- List of XML mapping files -->
<mapping resource="Host.hbm.xml" />
<mapping resource="HostGraph.hbm.xml" />
<mapping resource="HostTemplate.hbm.xml" />
<mapping resource="HostTemplateGraph.hbm.xml" />
<mapping resource="Settings.hbm.xml" />
</session-factory>
</hibernate-configuration>
这是我的计划:
public class SynchroCacti {
public static Map<String,Host> hosts = new HashMap<String, Host>();
private static SessionFactory factoryDerbi,factoryCacti;
private static Transaction tx=null;
private static Session sessionDerbi,sessionCacti;
public static List<Equipment> derbiEquipments;
private static List<HostTemplate> hostTemplates;
private static List<Settings> settings;
private static int nbNewHosts = 0;
private static int initialNbCactiHost,initialNbDerbiEqpts;
public static void initializeData ()
{
try{
factoryDerbi = new Configuration().configure("/hibernate_derbi.cfg.xml").buildSessionFactory();
factoryCacti = new Configuration().configure("/hibernate_cacti.cfg.xml").buildSessionFactory();
sessionDerbi = factoryDerbi.openSession();
sessionCacti = factoryCacti.openSession();
tx = sessionCacti.beginTransaction();
}catch (Throwable ex) {
logErrorsInProgram.error("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
//Store tables of the database in these lists
derbiEquipments = sessionDerbi.createCriteria(Equipment.class).list();
List<Host> cactiHosts = sessionCacti.createCriteria(Host.class).list();
hostTemplates = sessionCacti.createCriteria(HostTemplate.class).list();
settings = sessionCacti.createCriteria(Settings.class).list();
//Initialize the hash map of hosts
Iterator<Host> it = cactiHosts.iterator();
while (it.hasNext())
{
Host actual = it.next();
hosts.put(actual.getDescription(), actual);
}
//Initialize values of initialNbCactiHost and initialNbDerbiEqpts from sizes of lists
initialNbCactiHost = hosts.size();
initialNbDerbiEqpts = derbiEquipments.size();
}
public static void addHostToDatabase (Host host)
{
nbNewHosts++;
try{
sessionCacti.save(host);
}catch (ConstraintViolationException e) {
if (tx!=null) tx.rollback();
}finally { //We don't close session
}
}
public static void main(String[] args) throws HibernateException, SQLException, ParseException {
initializeData();
Host host = createHost(derbiEquipments.get(0));
addHostToDatabase(host);
}
}
在执行tx.commit();
之前,我不想保存数据
感谢。