Simple Update : Update « 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 » Update 




Simple Update

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

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class SimpleUpdateTest {
  
  
  public static void main(String[] args) {
    HibernateUtil.setup("create table EVENTS ( EVENT_ID int, EVENT_DATE date, title VARCHAR);");
    // hibernate code start
    Session session = HibernateUtil.currentSession();
    Transaction tx = session.beginTransaction();

    Event theEvent = new Event();
    theEvent.setTitle("My Event");
    theEvent.setDate(new Date());

    session.save(theEvent);

    tx.commit();
    HibernateUtil.closeSession();

    HibernateUtil.sessionFactory.close();
    // hibernate code end
    
    update();
    
    HibernateUtil.checkData("select * from events;");

  }
    public static void update(){
      Session session = HibernateUtil.currentSession();
      Transaction tx = session.beginTransaction();
    
      Event e = (Event)session.get(Event.class, new Long(1));
      
      e.setTitle("new Title");
      session.update(e);
      
      tx.commit();
      HibernateUtil.closeSession();
      
    }


}


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

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

<hibernate-mapping>
    <class name="Event" table="EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="increment"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
    </class>




    <class name="EventWithAttendee" table="eventwithattendee">
        <id name="id" column="uid" type="long" unsaved-value="null">
            <generator class="native"/>
        </id>
        <property name="name" type="string" length="100"/>
        <property name="startDate" column="start_date"
                  type="date"/>
        <property name="duration" type="integer"/>
        <many-to-one name="location" column="location_id"
                     class="Location"/>
        <set name="attendees" cascade="all">
            <key column="event_id"/>
            <one-to-many class="EventAttendee"/>
        </set>
    </class>

    <class name="EventAttendee" table="attendees">
        <id name="id" column="uid" type="long">
            <generator class="native"/>
        </id>
        <property name="firstName" type="string" length="20"/>
        <property name="lastName" type="string" length="20"/>
    </class>

    <class name="Location" table="locations">
        <id name="id" column="uid" type="long">
            <generator class="native"/>
        </id>
        <property name="name" type="string"/>
        <property name="address" type="string"/>
    </class>
</hibernate-mapping>



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


import java.util.Date;

public class Event {
  private Long id;

  private String title;

  private Date date;

  Event() {
  }

  public Long getId() {
    return id;
  }

  private void setId(Long id) {
    this.id = id;
  }

  public Date getDate() {
    return date;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String toString() {
    return "ID:" + id + "\nTitle:" + title + "\nDate:" + date;
  }
}


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

<?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:data/tutorial</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

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

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <mapping resource="Event.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

           
       














HibernateSimpleUpdate.zip( 4,582 k)
Related examples in the same category
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.