-->
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.  [ 4 posts ] 
Author Message
 Post subject: Getting Started With Annotations
PostPosted: Thu Jan 22, 2009 6:57 pm 
Newbie

Joined: Thu Jan 22, 2009 6:48 pm
Posts: 5
I am very new to Hibernate and Annotations and I'm also fairly new to programming. As of now I am simply trying to added a few things into the database and query them. When I do a transaction and add an item into the database, I don't get any errors, but when I query the table for what I added an empty list is returned. Below is my source code as of now.

setup
Quote:
package hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil
{
private static SessionFactory sessionFactory;

static
{
try
{
//properties im not very sure of......
AnnotationConfiguration config = new AnnotationConfiguration();
config.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
config.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
config.setProperty("hibernate.connection.url", "jdbc:hsqldb:file:db");
config.setProperty("hibernate.connection.username", "sa");
config.setProperty("hibernate.connection.password", "");
config.setProperty("hibernate.connection.pool_size", "1"); //for problems with hsql
config.setProperty("hibernate.connection.autocommit", "true");
//config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
config.setProperty("hibernate.hbm2ddl.auto", "create-drop");
config.setProperty("hibernate.show_sql", "true");
config.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
config.setProperty("hibernate.current_session_context_class", "thread");
config.setProperty("hibernate.hbm2ddl.auto", "create");

// Add your mapped classes here:
config.addAnnotatedClass(LoadoutPlan.class);

sessionFactory = config.buildSessionFactory();
}
catch (Throwable ex)
{
System.err.println("Initial SessionFactory creation failed." + ex);
}
}

public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}



Annotated Class
Quote:
import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name="loadout_table")
public class LoadoutPlan implements Serializable {

@Id
private String id;

@Column(name="planName")
private String planName;

@Column(name="shipName")
private String shipName;

public LoadoutPlan() {}

public String getId() {
return id;
}

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

public String getPlanName() {
return planName;
}

public void setPlanName(String planName) {
this.planName = planName;
}

public String getShipName() {
return shipName;
}

public void setShipName(String shipName) {
this.shipName = shipName;
}
}


Storing element
Quote:
import java.sql.Connection;
import java.sql.DriverManager;

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

public class InitiateHSQL {
public static void main(String args[]) throws Exception {

System.out.println("starting..");
//get a session
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();

//create a loadoutplan
LoadoutPlan plan = new LoadoutPlan();
plan.setId("plan5");
plan.setPlanName("The Bestest Plan1");
plan.setShipName("The Bigger Ship1");

System.out.println("storing..");
//save the loadout plan
session.save(plan);

session.getTransaction().commit();

System.out.println(plan.getId());

//close the session
session.close();

System.out.println("finished");
}

}


Query the database
Quote:
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hsqldb.Types;

import java.util.Iterator;
import java.util.List;

public class QueryPlans {
public static void main(String args[]) throws Exception {

//get a session
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

//start a query
SQLQuery query = session.createSQLQuery("SELECT * " +
"FROM loadout_table");

//iterate through the query
Iterator it = query.list().iterator();
System.out.println(it.hasNext());
while(it.hasNext()) {
System.out.println(it.next().getClass());
System.out.println("gemme something");
}


//cleanup
session.flush();
session.clear();

tx.commit();
session.close();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2009 11:13 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Likely your problem is this:

Code:
config.setProperty("hibernate.hbm2ddl.auto", "create-drop");


This tells Hibernate to create the db schema at startup and drop it at shutdown! You surely don't want that :) Use "update" instead.

Also, when you're querying, use this:

Code:
Query query = session.createQuery("from LoadoutPlan");


That will give you back a List of LoadoutPlan instances rather than a bunch of Object[].

Good luck.

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 4:28 pm 
Newbie

Joined: Thu Jan 22, 2009 6:48 pm
Posts: 5
changing the create-drop to update definitely seemed to change something, but I am still getting empty queries returned. Even when I use

Quote:
Query query = session.createQuery("from LoadoutPlan");


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 5:31 pm 
Newbie

Joined: Thu Jan 22, 2009 6:48 pm
Posts: 5
I've also noticed this from my logger.

Code:
2563 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - updating schema
2594 [main] INFO org.hibernate.tool.hbm2ddl.DatabaseMetadata - table not found: loadout_table
2594 [main] INFO org.hibernate.tool.hbm2ddl.DatabaseMetadata - table not found: loadout_table
2594 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - schema update complete


I have a feeling this is where my problem is. The schema generation can't find my previously created tables. Any ideas why?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.