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();
}
}