Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.hibenates.dto;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- @Entity
- public class UserDetails {
- @Id
- private int userId;
- private String userName;
- public int getUserId() {
- return userId;
- }
- public void setUserId(int userId) {
- this.userId = userId;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- }
- package org.hibenates;
- import org.hibenates.dto.UserDetails;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- public class HibernateTest {
- public static void main(String[] args) {
- UserDetails details = new UserDetails();
- details.setUserId(1);
- details.setUserName("First User");
- Session session = null;
- try {
- /* Create a Session factory */
- Configuration configuration = new Configuration();
- configuration.configure();
- ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
- .applySettings(configuration.getProperties()).build();
- SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
- /* Create sessions from session factory */
- session = sessionFactory.openSession();
- session.beginTransaction();
- /* Use the session to do the operations on model objects */
- session.save(details);
- } catch (Exception e) {
- System.out.println(e);
- } finally {
- session.getTransaction().commit();
- }
- /* close session */
- }
- }
- <?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>
- <!-- Database connection settings -->
- <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
- <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->
- <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
- <property name="connection.url">jdbc:sqlserver://U0138039-TPD-ASQLEXPRESS:1433;databaseName=HibernatesDataBase</property>
- <property name="connection.username">sa</property>
- <property name="connection.password">T!ger123</property>
- <!-- JDBC connection pool (use the built-in) -->
- <property name="connection.pool_size">1</property>
- <!-- SQL dialect -->
- <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
- <!-- Enable Hibernate's automatic session context management -->
- <property name="current_session_context_class">thread</property>
- <!-- Disable the second-level cache -->
- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
- <!-- Echo all executed SQL to stdout -->
- <property name="show_sql">true</property>
- <!-- Drop and re-create the database schema on startup -->
- <property name="hbm2ddl.auto">Create</property>
- <!-- Names the annotated entity Class -->
- <mapping class="org.hibenates.dto.UserDetails" />
- </session-factory>
- </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement