[ajug-members] Hibernate

Barnes, Michael MBarnes at tiaa-cref.org
Mon Nov 15 16:01:00 EST 2004


Thanks for all of your response. This appears to be to simplest example for
me to digest.

Thanks

-----Original Message-----
From: ajug-members-bounces at ajug.org
[mailto:ajug-members-bounces at ajug.org]On Behalf Of Narendra Kharya
Sent: Monday, November 15, 2004 3:04 PM
To: ajug-members at ajug.org
Subject: RE: [ajug-members] Hibernate


see if this is helpful...............


Create a class name Cat like -
package org.hibernate.auction;
public class Cat {

    private String id;
    private String name;
    private char sex;
    private float weight;

    public Cat() {
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

}

after creating the class make the mapping xml file name Cat.hbm.xml-
in the same directory.

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

<hibernate-mapping>

    <class name="org.hibernate.auction.Cat" table="CAT">

        <!-- A 32 hex character is our surrogate key. It's automatically
            generated by Hibernate with the UUID pattern. -->
        <id name="id" type="string" unsaved-value="null" >
            <column name="CAT_ID" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>

        <!-- A cat has to have a name, but it shouldn' be too long. -->
        <property name="name">
            <column name="NAME" length="16" not-null="true"/>
        </property>

        <property name="sex"/>

        <property name="weight"/>

    </class>

</hibernate-mapping>

once this file is made then make a class  with main method  --
public static void main(String[] args) throws Exception {
		final Main test = new Main();

		Configuration cfg = new Configuration()
			.addClass(Cat.class)
//this will create CAT table in the database using the mapping file.
			.setProperty(Environment.HBM2DDL_AUTO, "create");;

		//cfg.setProperty("hibernate.show_sql", "true");

		test.factory = cfg.buildSessionFactory();


		Session s = test.factory.openSession();
		Transaction tx=null;
		try {
			tx = s.beginTransaction();
			Cat princess = new Cat();
			princess.setName("Princess");
			princess.setSex('F');
			princess.setWeight(7.4f);
			s.save(princess) ;
			tx.commit();
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			s.close();
		}




Narendra B Kharya
617-501-2045(cell)




From: "Barnes, Michael" <MBarnes at tiaa-cref.org>
Reply-To: "General AJUG membership forum (100-200 
messages/month)"<ajug-members at ajug.org>
To: "'ajug-members at ajug.org'" <ajug-members at ajug.org>
Subject: [ajug-members] Hibernate
Date: Mon, 15 Nov 2004 14:16:26 -0500

I have just started on a project that will be using Hibernate and am looking
for a "Hello World" tutorial. Can someone recommend one that I can look use
to understand hibernate?

Thanks
Mike


**************************************************************
This message, including any attachments, contains confidential information 
intended for a specific individual and purpose, and is protected by law.  If

you are not the intended recipient, please contact sender immediately by 
reply e-mail and destroy all copies.  You are hereby notified that any 
disclosure, copying, or distribution of this message, or the taking of any 
action based on it, is strictly prohibited.
TIAA-CREF
**************************************************************

_______________________________________________
ajug-members mailing list
ajug-members at ajug.org
http://www.ajug.org/mailman/listinfo/ajug-members


_______________________________________________
ajug-members mailing list
ajug-members at ajug.org
http://www.ajug.org/mailman/listinfo/ajug-members


**************************************************************
This message, including any attachments, contains confidential information intended for a specific individual and purpose, and is protected by law.  If you are not the intended recipient, please contact sender immediately by reply e-mail and destroy all copies.  You are hereby notified that any disclosure, copying, or distribution of this message, or the taking of any action based on it, is strictly prohibited.
TIAA-CREF
**************************************************************




More information about the ajug-members mailing list