I'm converting an application from JPA to Hibernate and I already have some named queries defined in the pojos but for some reason they are not binded.
Here's my 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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/marauroa</property>
<property name="hibernate.connection.username">simple_user</property>
<property name="hibernate.connection.password">password</property>
<!-- Disable second-level cache. -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_minimal_puts">false</property>
<property name="max_fetch_depth">3</property>
<!-- Print SQL to stdout. -->
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<!-- Drop and then re-create schema on SessionFactory build, for testing. -->
<property name="hbm2ddl.auto">create</property>
<!-- Bind the getCurrentSession() method to the thread. -->
<property name="current_session_context_class">thread</property>
<mapping resource="simple/server/core/hibernate/Account.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/BanList.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/CharacterStat.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/DBCharacter.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/DBLoginEvent.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/DBRPObject.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/DBRPZone.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/GameEvent.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/ItemId.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/ItemLog.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/PasswordChange.hbm.xml"/>
<mapping resource="simple/server/core/hibernate/Statistic.hbm.xml"/>
</session-factory>
</hibernate-configuration>
And one of the POJO's as an example:
Code:
package simple.server.core.hibernate;
// Generated May 28, 2009 7:34:39 AM by Hibernate Tools 3.2.1.GA
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;
/**
* BanList generated by hbm2java
*/
@Entity
@Table(name = "banlist")
@NamedQueries({@NamedQuery(name = "BanList.findAll",
query = "SELECT b FROM BanList b"),
@NamedQuery(name = "BanList.findById",
query = "SELECT b FROM BanList b WHERE b.id = :id"),
@NamedQuery(name = "BanList.findByAddress",
query = "SELECT b FROM BanList b WHERE b.address = :address"),
@NamedQuery(name = "BanList.findByMask",
query = "SELECT b FROM BanList b WHERE b.mask = :mask"),
@NamedQuery(name = "BanList.findByReason",
query = "SELECT b FROM BanList b WHERE b.reason = :reason")})
public class BanList implements java.io.Serializable {
private Integer id;
private String address;
private String mask;
private String reason;
public BanList() {
}
public BanList(String address, String mask, String reason) {
this.address = address;
this.mask = mask;
this.reason = reason;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "address", length = 15)
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name = "mask", length = 15)
public String getMask() {
return this.mask;
}
public void setMask(String mask) {
this.mask = mask;
}
@Column(name = "reason")
public String getReason() {
return this.reason;
}
public void setReason(String reason) {
this.reason = reason;
}
}
My HibernateUtil should be correct based on stuff in the forums:
Code:
package simple.server.core.engine;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
* Hibernate Utility class with a convenient method to get Session Factory object.
*
* @author Javier A. Ortiz Bultron<javier.ortiz.78@gmail.com>
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
private static Transaction currentTransaction = null;
private static Session session = null;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
* Get the session factory
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
.
.
.
}
Note the
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); line which I though would take care of the annotations.
If I place them in the hbm.xml files it works but I thought I wouldn't need to transform them and use them straight from the POJO. Am I missing something?