Save Date Type : Hibernate Data Type « Hibernate « Java






Save Date Type

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

import java.util.Date;

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

public class SimpleSaveTest {
  
  
  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
    HibernateUtil.checkData("select * from events;");

  }


}



/////////////////////////////////////////////////////////////////////////
<?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;
  }
}

           
       








HibernateSaveDateType.zip( 4,581 k)

Related examples in the same category

1.Java Type VS Hibernate Type
2.Mapping Blob Binary Data Just Mapping (have bugs)
3.Float Data Type
4.Date Calendar Type Demo