1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| ***************************************************
package com.genuitec.hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class testHibernate {
public static void main(String[] args) {
// Step 1 - Create a new entity
String message = "msg";
// Step 2 - Set message field
//message.setMsg("Hello Hibernate, from MyEclipse!");
//Step 3 - Get a Hibernate Session
//Session sess;
SessionManager session = new SessionManager();
try {
session.currentSession();
// Step 4 - Persist entity to database
//Transaction tx = session.beginTransaction();
//session.save(message);
//tx.commit();
System.out.println("Save successful.");
} catch (HibernateException e) {
System.out.println("Save failed.");
} finally {
try {
// Step 5 - close the session
session.closeSession();
} catch (HibernateException e1) {
// do nothing
}
}
}
}
*****************************************************
package com.genuitec.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link <a href="http://hibernate.org/42.html" target="_blank">http://hibernate.org/42.html</a>}.
*/
public class SessionManager {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
//private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
public String profile="accord_dev";
public String dialect="org.hibernate.dialect.Oracle9Dialect";
public String url="jdbc:oracle:thin:@192.42.172.39:1521:pcardt";
public String username="accord_dev";
public String password="accord01";
public String driver_class="oracle.jdbc.driver.OracleDriver";
/** Holds a single instance of Session */
private ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
public final Configuration cfg = new Configuration()
.addClass(Client.class)
.addClass(Card.class)
.setProperty("connection.url",url)
.setProperty("dialect",dialect)
.setProperty("connection.username",username)
.setProperty("connection.password",password)
.setProperty("connection.driver_class",driver_class)
.setProperty("myeclipse.connection.profile",profile);
/** The single instance of hibernate SessionFactory */
private org.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
//cfg.configure();
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* Default constructor.
*/
public SessionManager() {
}
}
******************************************************* |
Partager