Here are the examples of the java api org.hibernate.SessionFactory.close() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
94 Examples
19
Source : HibernateUtil.java
with Apache License 2.0
from V1toss
with Apache License 2.0
from V1toss
/**
* Close session factory.
*/
public void closeFactory() {
factory.close();
}
19
Source : HibernateConfig.java
with Apache License 2.0
from tmobile
with Apache License 2.0
from tmobile
/**
* Function to close the connection to the database
*/
public static synchronized void closeSessionFactory() {
if (sessionFactory == null) {
return;
}
sessionFactory.close();
sessionFactory = null;
}
19
Source : DrugsDAOImpl.java
with GNU General Public License v2.0
from taktik
with GNU General Public License v2.0
from taktik
public void stopDrugsDatabase() {
try {
if (sessionFactory != null) {
sessionFactory.close();
sessionFactory = null;
}
if (indexSearcher != null) {
indexSearcher = null;
}
} catch (Exception e) {
// Ignore any exception at this stage.
}
}
19
Source : Database.java
with MIT License
from refactoring-ai
with MIT License
from refactoring-ai
// shutdown the session factory and all connections
public void shutdown() {
close();
sf.close();
sf = null;
}
19
Source : HibernateUtil.java
with GNU General Public License v2.0
from openkm
with GNU General Public License v2.0
from openkm
/**
* Close factory
*/
public static void closeSessionFactory() {
if (sessionFactory != null) {
sessionFactory.close();
sessionFactory = null;
}
}
19
Source : Hib.java
with Apache License 2.0
from guzhigang001
with Apache License 2.0
from guzhigang001
/**
* 关闭sessionFactory
*/
public static void closeFactory() {
if (sessionFactory != null) {
sessionFactory.close();
}
}
19
Source : TestHibernateConfigurator.java
with European Union Public License 1.1
from EUSurvey
with European Union Public License 1.1
from EUSurvey
public static void close() {
sessionFactory.close();
}
19
Source : DataSourceFactory.java
with GNU General Public License v2.0
from Coder-ACJHP
with GNU General Public License v2.0
from Coder-ACJHP
public void shutDown() {
if (sessionFactory != null && sessionFactory.isOpen())
sessionFactory.close();
}
18
Source : HibernatePersistenceProvider.java
with MIT License
from VoxelGamesLib
with MIT License
from VoxelGamesLib
@Override
public void disable() {
if (sessionFactory != null) {
sessionFactory.close();
}
}
18
Source : AbstractTest.java
with Apache License 2.0
from vladmihalcea
with Apache License 2.0
from vladmihalcea
@After
public void destroy() {
if (nativeHibernateSessionFactoryBootstrap()) {
sf.close();
} else {
emf.close();
}
}
18
Source : DefaultPersistManager.java
with MIT License
from theonedev
with MIT License
from theonedev
@Override
public void stop() {
if (sessionFactory != null) {
sessionFactory.close();
sessionFactory = null;
}
}
18
Source : HibernateDaoProvider.java
with GNU General Public License v3.0
from GravitLauncher
with GNU General Public License v3.0
from GravitLauncher
@Override
public void close() throws Exception {
sessionFactory.close();
}
18
Source : DatabaseServiceStorage.java
with Apache License 2.0
from exactpro
with Apache License 2.0
from exactpro
@Override
public void dispose() {
flusher.stop();
if (sessionFactory != null) {
sessionFactory.close();
}
}
18
Source : DatabaseMessageStorage.java
with Apache License 2.0
from exactpro
with Apache License 2.0
from exactpro
@Override
public void dispose() {
flusher.stop();
sessionFactory.close();
}
18
Source : HibernateUtil.java
with MIT License
from ConnectedPlacesCatapult
with MIT License
from ConnectedPlacesCatapult
public static void shutdown() {
// Close caches and connection pools
sharedSession.close();
sessionFactory.close();
}
18
Source : HibernateTest.java
with Apache License 2.0
from code4wt
with Apache License 2.0
from code4wt
@After
public void destroy() {
buildSessionFactory.close();
}
18
Source : ExampleMain.java
with MIT License
from biezhi
with MIT License
from biezhi
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
try {
persist(sessionFactory);
load(sessionFactory);
} finally {
sessionFactory.close();
}
}
17
Source : ORMReactivePersistenceTest.java
with GNU Lesser General Public License v2.1
from hibernate
with GNU Lesser General Public License v2.1
from hibernate
@After
public void cleanDb(TestContext context) {
ormFactory.close();
test(context, completedFuture(openSession()).thenCompose(s -> s.createQuery("delete Flour").executeUpdate()));
}
17
Source : Hibernate.java
with Apache License 2.0
from braisdom
with Apache License 2.0
from braisdom
@Override
public void teardown() {
dataSource.close();
sessionFactory.close();
}
16
Source : AbstractTest.java
with Apache License 2.0
from vladmihalcea
with Apache License 2.0
from vladmihalcea
@After
public void destroy() {
if (nativeHibernateSessionFactoryBootstrap()) {
sf.close();
} else {
emf.close();
}
for (Closeable closeable : closeables) {
try {
closeable.close();
} catch (IOException e) {
LOGGER.error("Failure", e);
}
}
closeables.clear();
}
16
Source : HibernateStoreTest.java
with Apache License 2.0
from peterarsentev
with Apache License 2.0
from peterarsentev
/**
* Create a new user and check id.
*/
@Test
public void whenCreateUser() {
SessionFactory factory = create(HibernateFactory.FACTORY);
HibernateStore store = new HibernateStore(factory);
User user = store.add(new User(-1, "Petr Arsentev"));
replacedertThat(user.getId(), not(-1));
factory.close();
}
16
Source : SessionFactoryRegistry.java
with GNU General Public License v2.0
from lamsfoundation
with GNU General Public License v2.0
from lamsfoundation
public void clearRegistrations() {
nameUuidXref.clear();
for (SessionFactory factory : sessionFactoryMap.values()) {
try {
factory.close();
} catch (Exception ignore) {
}
}
sessionFactoryMap.clear();
}
16
Source : BaseReactiveTest.java
with GNU Lesser General Public License v2.1
from hibernate
with GNU Lesser General Public License v2.1
from hibernate
@After
public void after(TestContext context) {
if (session != null && session.isOpen()) {
session.close();
session = null;
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
} finally {
connection = null;
}
}
if (sessionFactory != null) {
sessionFactory.close();
}
}
16
Source : SampleApplication.java
with GNU Lesser General Public License v2.1
from GoogleCloudPlatform
with GNU Lesser General Public License v2.1
from GoogleCloudPlatform
/**
* Main method that runs a simple console application that saves a {@link Person} enreplacedy and then
* retrieves it to print to the console.
*/
public static void main(String[] args) {
// Create Hibernate environment objects.
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
try {
// Save a Person enreplacedy into Spanner Table.
savePerson(session);
// Save a singer enreplacedy into the Spanner Table.
saveSingerAlbum(session);
} finally {
session.close();
sessionFactory.close();
}
}
15
Source : DrugsDAOImpl.java
with GNU General Public License v2.0
from taktik
with GNU General Public License v2.0
from taktik
public void installNewDrugsDatabase(String zipFile) {
try {
if (sessionFactory != null) {
sessionFactory.close();
sessionFactory = null;
}
if (indexSearcher != null) {
indexSearcher = null;
}
File fdbDir = getDbDir();
if (fdbDir.exists()) {
File oldDb = new File(fdbDir.getParentFile(), "drugs.old");
if (oldDb.exists()) {
FileUtils.deleteDirectory(oldDb);
}
fdbDir.renameTo(oldDb);
}
if (!fdbDir.mkdir()) {
throw new RuntimeException("Unable to create Drugs dir!");
}
Unzip.unzip(new File(zipFile), fdbDir);
} catch (Exception e) {
throw new RuntimeException("Unable to install new Drugs database", e);
}
}
15
Source : HibernateTest.java
with Apache License 2.0
from opentracing-contrib
with Apache License 2.0
from opentracing-contrib
@Test
public void hibernate() {
SessionFactory sessionFactory = createSessionFactory(false);
Session session = sessionFactory.openSession();
Employee employee = new Employee();
session.beginTransaction();
session.save(employee);
session.getTransaction().commit();
session.close();
sessionFactory.close();
replacedertNotNull(employee.id);
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
replacedertEquals(14, finishedSpans.size());
checkSpans(finishedSpans, "hibernate");
replacedertNull(mockTracer.activeSpan());
}
15
Source : HibernateTest.java
with Apache License 2.0
from opentracing-contrib
with Apache License 2.0
from opentracing-contrib
@Test
public void hibernate_with_ignored_statement() {
SessionFactory sessionFactory = createSessionFactory(false, Collections.singletonList("insert into Employee (id) values (?)"));
Session session = sessionFactory.openSession();
Employee employee = new Employee();
session.beginTransaction();
session.save(employee);
session.getTransaction().commit();
session.close();
sessionFactory.close();
replacedertNotNull(employee.id);
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
replacedertEquals(13, finishedSpans.size());
checkSpans(finishedSpans, "hibernate");
replacedertNull(mockTracer.activeSpan());
}
15
Source : HibernateTest.java
with Apache License 2.0
from opentracing-contrib
with Apache License 2.0
from opentracing-contrib
@Test
public void hibernate_with_active_span_only() {
SessionFactory sessionFactory = createSessionFactory(true);
Session session = sessionFactory.openSession();
Employee employee = new Employee();
session.beginTransaction();
session.save(employee);
session.getTransaction().commit();
session.close();
sessionFactory.close();
replacedertNotNull(employee.id);
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
replacedertEquals(0, finishedSpans.size());
replacedertNull(mockTracer.activeSpan());
}
15
Source : HibernateContextDAO.java
with Apache License 2.0
from isstac
with Apache License 2.0
from isstac
/**
* @see org.openmrs.api.context.Context#shutdown()
*/
@Override
public void shutdown() {
if (log.isInfoEnabled()) {
showUsageStatistics();
}
if (sessionFactory != null) {
log.debug("Closing any open sessions");
closeSession();
log.debug("Shutting down threadLocalSession factory");
if (!sessionFactory.isClosed()) {
sessionFactory.close();
}
log.debug("The threadLocalSession has been closed");
} else {
log.error("SessionFactory is null");
}
}
14
Source : Main.java
with BSD 3-Clause "New" or "Revised" License
from ohbus
with BSD 3-Clause "New" or "Revised" License
from ohbus
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure("hibernate1.cfg.xml").addAnnotatedClreplaced(Car.clreplaced).buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
/*Car c=new Car();
c.setRegno("sa");
c.setManufacturer("hello");
c.setColor("yellow");
c.setModel("swift");
session.saveOrUpdate(c);*/
session.getTransaction().commit();
sessionFactory.close();
}
14
Source : App.java
with GNU Lesser General Public License v2.1
from GoogleCloudPlatform
with GNU Lesser General Public License v2.1
from GoogleCloudPlatform
/**
* The main method that does the CRUD operations.
*/
public static void main(String[] args) {
// create a Hibernate sessionFactory and session
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
clearData(session);
writeData(session);
readData(session);
// close Hibernate session and sessionFactory
session.close();
sessionFactory.close();
}
13
Source : HibernateStoreTest.java
with Apache License 2.0
from peterarsentev
with Apache License 2.0
from peterarsentev
/**
* Create a new user and find it.
*/
@Test
public void whenCreateAndFind() {
SessionFactory factory = create(HibernateFactory.FACTORY);
Session session = factory.openSession();
HibernateStore store = new HibernateStore(factory);
User user = store.add(new User(-1, "Petr Arsentev"));
replacedertThat(store.findById(user.getId()).getLogin(), is("Petr Arsentev"));
session.clear();
replacedertThat(store.findAll().isEmpty(), is(true));
factory.close();
}
13
Source : HibernateTest.java
with Apache License 2.0
from opentracing-contrib
with Apache License 2.0
from opentracing-contrib
@Test
public void hibernate_with_parent() {
final MockSpan parent = mockTracer.buildSpan("parent").start();
try (Scope ignored = mockTracer.activateSpan(parent)) {
SessionFactory sessionFactory = createSessionFactory(false);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(new Employee());
session.save(new Employee());
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
parent.finish();
List<MockSpan> spans = mockTracer.finishedSpans();
replacedertEquals(17, spans.size());
checkSameTrace(spans);
replacedertNull(mockTracer.activeSpan());
}
13
Source : HibernateTest.java
with Apache License 2.0
from opentracing-contrib
with Apache License 2.0
from opentracing-contrib
@Test
public void hibernate_with_parent_and_active_span_only() {
final MockSpan parent = mockTracer.buildSpan("parent").start();
try (Scope ignored = mockTracer.activateSpan(parent)) {
SessionFactory sessionFactory = createSessionFactory(true);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(new Employee());
session.save(new Employee());
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
parent.finish();
List<MockSpan> spans = mockTracer.finishedSpans();
replacedertEquals(17, spans.size());
checkSameTrace(spans);
replacedertNull(mockTracer.activeSpan());
}
13
Source : Main.java
with BSD 3-Clause "New" or "Revised" License
from ohbus
with BSD 3-Clause "New" or "Revised" License
from ohbus
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure("hibernate4.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Product p1 = new Product("Austex", 101, 20000);
session.save(p1);
Product p2 = new Product("Baby Donuts", 110, 10000);
session.save(p2);
Product p3 = new Product("Billion Air", 200, 30000);
session.save(p3);
Product p4 = new Product("Coffeebags", 300, 40000);
session.save(p4);
Product p5 = new Product("Farm Glue", 400, 50000);
session.save(p5);
session.getTransaction().commit();
sessionFactory.close();
}
13
Source : Main.java
with BSD 3-Clause "New" or "Revised" License
from ohbus
with BSD 3-Clause "New" or "Revised" License
from ohbus
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure("hibernate3.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Employee e1 = new Employee("Roy", "Trainee", 20000);
session.save(e1);
Employee e2 = new Employee("William", "Receptionist", 10000);
session.save(e2);
Employee e3 = new Employee("Charles", "Office Manager", 30000);
session.save(e3);
Employee e4 = new Employee("Donald", "Branch Manager", 40000);
session.save(e4);
Employee e5 = new Employee("George", "Business replacedyst", 50000);
session.save(e5);
session.getTransaction().commit();
sessionFactory.close();
}
13
Source : Main.java
with BSD 3-Clause "New" or "Revised" License
from ohbus
with BSD 3-Clause "New" or "Revised" License
from ohbus
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure("hibernate2.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Flower f = new Flower("F1", "Rose", "Red", 10);
session.save(f);
session.getTransaction().commit();
sessionFactory.close();
}
13
Source : Main.java
with BSD 3-Clause "New" or "Revised" License
from ohbus
with BSD 3-Clause "New" or "Revised" License
from ohbus
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure("hibernate1.cfg.xml").addAnnotatedClreplaced(Car.clreplaced).buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Car c1 = new Car("KL-07", "AB 123 Polo", "White", "Volkswagen");
session.save(c1);
Car c2 = new Car("KL-07", "AB 234 Vento", "Black", "Volkswagen");
session.save(c2);
Car c3 = new Car("KL-07", "AC 345 Corolla", "Silver", "Toyota");
session.save(c3);
session.getTransaction().commit();
sessionFactory.close();
}
13
Source : QueueTest.java
with Creative Commons Zero v1.0 Universal
from CMSgov
with Creative Commons Zero v1.0 Universal
from CMSgov
@AfterEach
void shutdown() {
try (final Session session = sessionFactory.openSession()) {
final Transaction tx = session.beginTransaction();
try {
session.createQuery("delete from job_queue_batch_file").executeUpdate();
session.createQuery("delete from job_queue_batch").executeUpdate();
} finally {
tx.commit();
}
}
sessionFactory.close();
}
13
Source : DataBase.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
public static boolean DeleteUser(String username) {
System.out.println(" deleteUser() ... ");
if (!isUserNameValid(username)) {
System.out.println("not valid username");
return false;
}
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(User.clreplaced).buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// now get a new session and start transaction
session = factory.getCurrentSession();
session.beginTransaction();
System.out.println("Deleting User username = '" + username + "'");
int executeUpdate = session.createQuery("delete from User where user_name = '" + username + "'").executeUpdate();
// commit the transaction
session.getTransaction().commit();
System.out.println("deleteGuest() Done!");
} catch (Exception e) {
e.printStackTrace();
} finally {
factory.close();
}
return true;
}
13
Source : Room.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
public static int CheckOut(int RoomId) {
System.out.println("in CheckOut() ...");
int flag = 1;
System.out.println("Excute deleteGuest() ... ");
Guest.deleteGuest(RoomId);
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(Room.clreplaced).buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// now get a new session and start transaction
session = factory.getCurrentSession();
session.beginTransaction();
// retrieve room based on the id: primary key
System.out.println("\nGetting room with id: " + RoomId);
Room room = (Room) session.get(Room.clreplaced, RoomId);
if (room == null) {
return -1;
} else {
if (room.isIsEmpty() == true) {
flag = 0;
}
System.out.println("Updating Room...");
room.setIsEmpty(true);
room.setCheck_Out_Date(new Date());
}
// commit the transaction
session.getTransaction().commit();
System.out.println("CheckOut Done!");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
factory.close();
}
return flag;
}
13
Source : Room.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
public static void CheckIn(Guest guest, Room r, int RoomId) {
System.out.println("in Checkin() ...");
// save guest
System.out.println("SaveGuest() ...");
Guest.SaveGuest(guest);
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(Room.clreplaced).buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// now get a new session and start transaction
session = factory.getCurrentSession();
session.beginTransaction();
// retrieve room based on the id: primary key
System.out.println("\nGetting room with id: " + RoomId);
Room room = (Room) session.get(Room.clreplaced, RoomId);
System.out.println("Updating Room...");
room.setIsEmpty(r.isIsEmpty());
room.setCheck_In_Date(r.getCheck_In_Date());
room.setCheck_Out_Date(r.getCheck_Out_Date());
// commit the transaction
session.getTransaction().commit();
System.out.println("CheckIn Done!");
} finally {
factory.close();
}
}
12
Source : DataBase.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
// =====================================
// guests
public static List<Guest> getGuests() {
List<Guest> guests = null;
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(Guest.clreplaced).buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// start a transaction
session.beginTransaction();
// query Guest
guests = session.createQuery("from Guest").list();
// display the Guest
displayList(guests);
// commit transaction
session.getTransaction().commit();
System.out.println("getGuest() Done!");
} finally {
factory.close();
}
return guests;
}
12
Source : DataBase.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
public static List<Room> getAvailableRooms() {
List<Room> rooms = null;
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(Room.clreplaced).buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// start a transaction
session.beginTransaction();
// query students
rooms = session.createQuery("from Room r where r.isEmpty=true").list();
// display the students
displayList(rooms);
session.getTransaction().commit();
System.out.println("getRooms() Done!");
} finally {
factory.close();
}
return rooms;
}
12
Source : DataBase.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
public static List<User> getUsers() {
List<User> users = null;
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(User.clreplaced).buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// start a transaction
session.beginTransaction();
// query users
users = session.createQuery("from User").list();
// display the users
displayList(users);
// commit transaction
session.getTransaction().commit();
System.out.println("getUsers() Done!");
} finally {
factory.close();
}
return users;
}
12
Source : DataBase.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
public static List<Room> getrooms() {
List<Room> rooms = null;
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(Room.clreplaced).buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// start a transaction
session.beginTransaction();
// query students
rooms = session.createQuery("from Room").list();
// display the students
displayList(rooms);
session.getTransaction().commit();
System.out.println("getRooms() Done!");
} finally {
factory.close();
}
return rooms;
}
12
Source : DataBase.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
public void ReadRoom(Room room) {
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(Room.clreplaced).buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// find out the student's id: primary key
System.out.println("Saved student. Generated id: " + room.getRoomID());
// now get a new session and start transaction
session = factory.getCurrentSession();
session.beginTransaction();
// retrieve student based on the id: primary key
System.out.println("\nGetting student with id: " + room.getRoomID());
Room r = (Room) session.get(Room.clreplaced, room.getRoomID());
System.out.println("Get complete: " + r);
// commit the transaction
session.getTransaction().commit();
System.out.println("Done!");
} finally {
factory.close();
}
}
12
Source : Guest.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
public static void deleteGuest(int roomID) {
System.out.println(" deleteGuest() ... ");
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(Guest.clreplaced).buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// now get a new session and start transaction
session = factory.getCurrentSession();
session.beginTransaction();
System.out.println("Deleting guest roomID = " + roomID);
session.createQuery("delete from Guest where roomID=" + roomID).executeUpdate();
// commit the transaction
session.getTransaction().commit();
System.out.println("deleteGuest() Done!");
} catch (Exception e) {
e.printStackTrace();
} finally {
factory.close();
}
}
12
Source : Guest.java
with MIT License
from AbdelrahmanBayoumi
with MIT License
from AbdelrahmanBayoumi
/*==========================================================================
------------------------((((DataBase)))))----------------------------------
==========================================================================*/
public static void SaveGuest(Guest guest) {
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClreplaced(Guest.clreplaced).buildSessionFactory();
System.out.println("create session factory");
// create session
Session session = factory.getCurrentSession();
System.out.println("create session");
try {
// start a transaction
session.beginTransaction();
System.out.println("start a transaction");
// save the guest object
System.out.println("Saving the guest...");
session.save(guest);
// commit transaction
session.getTransaction().commit();
System.out.println("Done!");
} catch (Exception e) {
System.out.println("SaveGuest Error");
} finally {
factory.close();
}
}
11
Source : InHibernate.java
with MIT License
from TranswarpCN
with MIT License
from TranswarpCN
// 主函数
public static void main(String[] args) {
String path = "hibernate.cfg.xml";
Configuration cfg = new Configuration().configure(path);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
User user = new User();
user.setId("443");
user.setName("baa");
session.save(user);
// session.close();
session.getTransaction().commit();
sessionFactory.close();
}
See More Examples