I am using hibernate 3 and postgresql database.
I am using the below classes and xml:
Expand|Select|Wrap|Line Numbers
- package org.javabrains.koushik.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;
- }
- }
Expand|Select|Wrap|Line Numbers
- package org.koushik.hibernate;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import org.javabrains.koushik.dto.UserDetails;
- public class HibernateTest {
- /**
- * @param args
- */
- static Session session;
- public static void main(String[] args) {
- UserDetails user=new UserDetails();
- user.setUserId(1);
- user.setUserName("First User");
- try
- {
- SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
- session=sessionFactory.openSession();
- session.beginTransaction();
- session.save(user);
- }
- catch(Exception e)
- {
- session.getTransaction().commit();
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- <?xml version="1.0" encoding="utf-8" ?>
- - <!-- ~ Hibernate, Relational Persistence for Idiomatic Java
- ~
- ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
- ~ indicated by the @author tags or express copyright attribution
- ~ statements applied by the authors. All third-party contributions are
- ~ distributed under license by Red Hat Inc.
- ~
- ~ This copyrighted material is made available to anyone wishing to use, modify,
- ~ copy, or redistribute it subject to the terms and conditions of the GNU
- ~ Lesser General Public License, as published by the Free Software Foundation.
- ~
- ~ This program is distributed in the hope that it will be useful,
- ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
- ~ for more details.
- ~
- ~ You should have received a copy of the GNU Lesser General Public License
- ~ along with this distribution; if not, write to:
- ~ Free Software Foundation, Inc.
- ~ 51 Franklin Street, Fifth Floor
- ~ Boston, MA 02110-1301 USA
- -->
- <!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.postgresql.Driver</property>
- <property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
- <property name="connection.username">postgres</property>
- <property name="connection.password">password</property>
- - <!-- JDBC connection pool (use the built-in)
- -->
- <property name="connection.pool_size">1</property>
- - <!-- SQL dialect
- -->
- <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
- - <!-- Disable the second-level cache
- -->
- <property name="cache.provider_class">org.hibernate.cache.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.javabrains.koushik.dto.UserDetails" />
- </session-factory>
- </hibernate-configuration>