-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: My first Hibernate
PostPosted: Fri Feb 27, 2004 9:28 am 
Newbie

Joined: Fri Feb 27, 2004 9:26 am
Posts: 1
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 9:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please take a look at some of the tutorials under http://www.hibernate.org/78.html, they are more step by step


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.