Hi,
nachdem ich jetzt schon stundenlang diverse Bücher zu Hibernate durchforste und meinen Fehler nicht finde. Von der Idee her bin ich begeistert, aber bringt mir nix wenn ich selber zu blöd bin das zum Laufen zu bringen... Was mache ich bei meinem Mapping falsch.
Aus der HQL-Anfrage "from User" ergibt sich bei mir folgendes SQL-Statement "select user0_.uid as uid47_, user0_.username as username47_, user0_.password as password47_, user0_.isadmin as isadmin47_, user0_.classid as classid47_, user0_.level as level47_, user0_.punkte as punkte47_, user0_.raider as raider47_
from User user0_".
Mit dem SQL-Statement bekomme ich eine org.hibernate.exception.SQLGrammarException. Soweit ich das sehen kann hat er damit auch völlig Recht, weil das ist kein gültiges Statement was Hibernate da generiert hat. Die Frage ist nur warum macht er das?
Ich benutze die Hibernate 3.2 Core Librarys mit Java 6 und PorstgreSQL 8.2. Die Hibernate-spezifischen Dateien sind:
hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">postgres</property>
<property name="connection.url">
jdbc:postgresql://localhost/TwixKoD
</property>
<property name="connection.username">twix</property>
<property name="connection.password">bin14qw#</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<property name="show_sql">true</property>
<mapping resource="org/obster/TwixKoD/data/user.hbm.xml" />
</session-factory>
</hibernate-configuration>
user.hbm.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.obster.TwixKoD.data.User" table="User">
<id name="id" column="uid" type="integer">
<generator class="assigned"/>
</id>
<property name="username" column="username" type="string"/>
<property name="password" column="password" type="string"/>
<property name="isAdmin" column="isadmin" type="boolean" not-null="true"/>
<property name="klasseId" column="classid" type="integer"/>
<property name="level" column="level" type="integer"/>
<property name="punkte" column="punkte" type="integer"/>
<property name="isRaider" column="raider" type="boolean"/>
</class>
</hibernate-mapping>
User.java:
Code:
public class User {
private int uid;
private String username;
private String password;
private boolean isAdmin;
private int klasseId;
private int level;
private int punkte;
private boolean isRaider;
public int getId(){
return this.uid;
}
public void setId(int id){
this.uid = id;
}
public String getUsername(){
return this.username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){
this.password = password;
}
public boolean getisAdmin(){
return this.isAdmin;
}
public void setisAdmin(boolean isAdmin){
this.isAdmin = isAdmin;
}
public int getKlasseId(){
return this.klasseId;
}
public void setKlasseId(int klasse){
this.klasseId = klasse;
}
public int getLevel(){
return this.level;
}
public void setLevel(int level){
this.level = level;
}
public int getPunkte(){
return this.punkte;
}
public void setPunkte(int punkte){
this.punkte = punkte;
}
public boolean getisRaider(){
return this.isRaider;
}
public void setisRaider(boolean isRaider){
this.isRaider = isRaider;
}
}
HibernateSessionFactory.java:
Code:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
HibernateUserDelegate.java:
Code:
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.obster.TwixKoD.data.User;
public class HibernateUserDelegate implements UserDelegate {
private SessionFactory sessionFactory;
public HibernateUserDelegate(){
System.out.println("#################################");
System.out.println("Init Hibernate");
sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("Finished Init Hibernate");
System.out.println("#################################");
}
public boolean login(String user, String password) {
Session sess = null;
Transaction trx = null;
User userobj = null;
try {
sess = sessionFactory.openSession();
trx = sess.beginTransaction();
System.out.println("Versuche Login zu lesen!" + user + password);
Query q = sess.createQuery("from User where username = 'mike'");
Iterator iter = q.iterate();
if (iter.hasNext()) {
userobj = (User) iter.next();
}
trx.commit();
if (userobj.getPassword().equals(password))
return true;
else
return false;
} catch (HibernateException ex) {
if( trx != null )
try {
trx.rollback();
} catch( HibernateException exRb ) {}
throw new RuntimeException( ex.getMessage() );
} finally {
try {
if( sess != null )
sess.close();
} catch( Exception exCl ) {}
}
}
}
DANKE und Gruss,
Michael