Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>5.0.6.Final</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-c3p0</artifactId>
- <version>5.0.6.Final</version>
- </dependency>
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.4</version>
- </dependency>
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/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.connection.url">jdbc:mysql://localhost/testprojectdatabase</property>
- <property name="hibernate.connection.username">username_here</property>
- <property name="hibernate.connection.password">password_here</property>
- <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
- <property name="hibernate.c3p0.min_size">5</property>
- <property name="hibernate.c3p0.max_size">20</property>
- <property name="hibernate.c3p0.timeout">300</property>
- <property name="hibernate.c3p0.max_statements">50</property>
- <mapping resource="milkiv/mytestproject/models/UserInfo.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
- public class UserInfoManager implements ManagerAdd<UserInfo> {
- private final SessionFactory factory;
- public UserInfoManager() {
- factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
- }
- public int add(UserInfo user) {
- Transaction transaction = null;
- Integer userId = null;
- try (Session session = factory.openSession()){
- transaction = session.beginTransaction();
- userId = (Integer) session.save(user);
- transaction.commit();
- } catch (HibernateException he) {
- if (transaction != null) {
- transaction.rollback();
- }
- }
- return userId;
- }
- }
- <mapping resource="/milkiv/mytestproject/models/UserInfo.hbm.xml"/>
- <mapping resource="UserInfo.hbm.xml"/>
- public UserInfoManager() {
- System.out.println(UserInfoManager.class
- .getResource("/milkiv/mytestproject/models/UserInfo.hbm.xml"));
- System.out.println(ClassLoader.getSystemClassLoader().getResource(
- "milkiv/mytestproject/models/UserInfo.hbm.xml"));
- }
- new UserInfoManager();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement