Key Int Auto Increment : Key « Hibernate « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Hibernate » Key 




Key Int Auto Increment

/////////////////////////////////////////////////////////////////////////

import org.hibernate.Session;

import util.ConnectionUtil;
import util.HibernateUtil;

public class Main {
  static Session session;

  public static void main(String[] args) {
    open();

    Keyword k = new Keyword();
    session.save(k);
    session.flush();
    
    close();
  }

  
  private static void open() {
    ConnectionUtil.openConnection();
    ConnectionUtil.update("CREATE TABLE KEYWORDS ( ID int, NAME VARCHAR(25) )");
    session = HibernateUtil.currentSession();
  }

  private static void close() {
    ConnectionUtil.closeConnection();
    HibernateUtil.closeSession();
    HibernateUtil.sessionFactory.close();
  }
}



/////////////////////////////////////////////////////////////////////////

public class Keyword {

    protected Integer    id_;
    protected String name_ ="aaa";

    public Keyword() {
    }

    public Integer getId() {
        return id_;
    }

    public void setId(Integer newId) {
        this.id_ = newId;
    }

    public String getName() {
        return name_;
    }

    public void setName(String newName) {
        this.name_ = newName;
    }
}



/////////////////////////////////////////////////////////////////////////

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <!-- table created by: CREATE TABLE KEYWORDS ID IDENTITY, NAME VARCHAR(25) ); -->
  <class name="Keyword" table="keywords">
        
    <id name="id" column="id" unsaved-value="null">
      <generator class="increment"/>
    </id>
        
    <property name="name" 
              column="NAME" 
              not-null="true" 
              unique="true"
              />
  </class>
</hibernate-mapping> 

<!-- $Id: Keyword.hbm.xml,v 1.3 2003/07/08 19:18:30 dwight Exp $ -->



/////////////////////////////////////////////////////////////////////////


<!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:mem:testdb</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in-->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</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>-->

        <mapping resource="Keyword.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


           
       














Hibernate-Key-Int-Auto-Increment.zip( 4,578 k)
Related examples in the same category
1.Key Generator UUID HEX
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.