Hi Fabio,

try reposting to the beginner forum at:

http://forum.hibernate.org

Thanks!

Fabio Esposito wrote:

Hello people!

I'm begin in the world of database persistence and i'm lost.... help!

My english isn't so good (i'm brazilian) but i will try to explain

In the section quickstart of the manual I don't understand how cat.class and
cat.hbm.xml work (I understand the structure, but i don't know how show my
db

Follow my files:
------------ cat.java
public class Cat {
private String id;
private String name;
private char sex;
private float weight;

public Cat() {
}

public String getId() {
 return id;
}

public 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;
}
}
-----------------------
------------------ cat.hbm.xml
<?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="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" sql-type="varchar(16)" not-null="true"/>
 </property>
 <property name="sex"/>
 <property name="weight"/>
</class>
</hibernate-mapping>
--------------------
-------- hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd";>

<hibernate-configuration>
<session-factory>
 <property
name="connection.datasource">java:comp/env/jdbc/quickstart</property>
 <property name="show_sql">false</property>
 <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

 <!-- Mapping files -->
 <mapping resource="Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>
---------------------------

------------- hibernate.properties
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class org.gjt.mm.mysql.Driver
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:/localhost/quickstart
hibernate.connection.username root
hibernate.connection.password
------------------------------

--------- hiernateutil.java

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletException;
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;

public class HibernateUtil
   extends HttpServlet {

   private SessionFactory sessionFactory;
   private Session session;
   private Transaction transaction;

   public void doGet(HttpServletRequest request, HttpServletResponse
response)
       throws ServletException, IOException {

       try {
           // Initialize Hibernate (Configuration and SessionFactory)
            initHibernate();

           // Prepare out
           response.setContentType("text/html");
           PrintWriter out = response.getWriter();

           // Create some Cats
           beginTransaction();
           createCats(out);
           endTransaction(true);

           // Select all Cats
           beginTransaction();
           selectAllCats(out);
           endTransaction(false);

           // Select female Cats
           beginTransaction();
           selectFemaleCats(out);
           endTransaction(false);

       } catch (HibernateException e) {
            throw new ServletException(e);
        }

}

   public void createCats(PrintWriter out)
       throws HibernateException {

       out.print("<h3>Creating Cats:</h3>");
       out.println("CREATING 'Princess'...<br/>");
       Cat princess = new Cat();
       princess.setName("Princess");
       princess.setSex('F');
       princess.setWeight(7.4f);
       session.save(princess);

       out.println("CREATING 'Max'...<br/>");
       Cat max = new Cat();
       max.setName("Max");
       max.setSex('M');
       max.setWeight(8.1f);
       session.save(max);

       out.println("CREATING 'Sophie'...<br/>");
       Cat sophie = new Cat();
       sophie.setName("Sophie");
       sophie.setSex('F');
       sophie.setWeight(4.1f);
       session.save(sophie);
   }

   public void selectAllCats(PrintWriter out)
       throws HibernateException {

       out.print("<h3>Retrieving all Cats:</h3>");
       String queryString = "select cat from Cat as cat";
       Query query = session.createQuery(queryString);
       for (Iterator it = query.iterate(); it.hasNext();) {
           Cat cat = (Cat) it.next();
           out.println("CAT: " + cat.getName() + " (" + cat.getSex() + ", "
+ cat.getWeight() + ")<br/>");
       }
   }

   public void selectFemaleCats(PrintWriter out)
       throws HibernateException {

       out.print("<h3>Retrieving female Cats:</h3>");
       String queryString = "select cat from Cat as cat where cat.sex =
:sex";
       Query query = session.createQuery(queryString);
       query.setCharacter("sex", 'F');
       for (Iterator it = query.iterate(); it.hasNext();) {
           Cat cat = (Cat) it.next();
           out.println("CAT: " + cat.getName() + " (" + cat.getSex() + ", "
+ cat.getWeight() + ")<br/>");
       }
   }

   // Helper Methods
   private void initHibernate()
       throws HibernateException {

       // Load Configuration and build SessionFactory
       sessionFactory = new
Configuration().configure().buildSessionFactory();
   }

   private void beginTransaction()
       throws HibernateException {

       session = sessionFactory.openSession();
       transaction = session.beginTransaction();
   }

   private void endTransaction(boolean commit)
       throws HibernateException {

       if (commit) {
           transaction.commit();
       } else {
           // Don't commit the transaction, can be faster for read-only
operations
           transaction.rollback();
       }
       session.close();
   }

}

--------------------------

All these files are in the C:\Tomcat\webapps\quickstart\WEB-INF\classes

I'm sorry if the questions are so easy, but I don't find any example step by
step

Thanks a lot
Fabio Esposito



-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel




--
Gavin King
JBoss Group
+61 410534454
http://hibernate.org



-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to