Hibernate doesn't work anymore... why? - java

I was doing simple things with hibernate, as I have to learn it for a project. I created this simple example:
package hibtests;
import hibtests.beans.newBean;
import org.hibernate.Session;
/**
*
* #author dario
*/
public class Main {
public void test(){
Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
newBean nb = new newBean();
nb.setNome("FooFoo");
session.save(nb);
session.getTransaction().commit();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main main = new Main();
main.test();
}
}
...and it was working fine, putting rows in the db. Then I worked on another class for a couple of hours. I try this example again and Hibernate makes this strange query:
Hibernate:
insert
into
TEST
(ID, NOME)
values
(default, ?)
Hibernate:
values
identity_val_local()
like it just can't read the property that is FooFoo. I checked if I changed the source... but it is not the case. Everything is just like before and there are not exceptions. The newBean instance is not null and FooFoo is in the Nome field. Why this?
Oh, I forgot, I'm using Netbeans 6.8 and JavaDB.
As requested, my mapping follows:
<?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="hibtests.beans.newBean" table="APP.TEST">
<id name="id" column="ID">
<generator class="identity"/>
</id>
<property name="nome" column="NOME" type="string"/>
</class>
</hibernate-mapping>
Last minute update: turns out that insertion is working. Anyway I can still see the query with a ? instead of the string. Why?
As requested newbean source code follows:
public class newBean {
Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
String nome;
}

You will never see the value of the strings being inserted in the DB, you will always see them as question marks (?) there are sniffers that will show their contents, but in standard hibernate you will not see any values.

Related

java.sql.SQLException: ResultSet may only be accessed in a forward direction

I have a program that works smoothly on MySQL database. Now for external requirements, I must switch to SQL Server. Thanks to Hibernate the switch was smooth, except for the error:
java.sql.SQLException: ResultSet may only be accessed in a forward direction.
I am getting this error when performing a custom pagination on the data that I get from the database. Below is a minimal example that tries to get and paginate the users stored in the database (the following example assumes that there are at least 3 rows in the table).
Custom pagination:
import org.hibernate.Query;
import org.hibernate.Session;
public class newMain1 {
public static void main(String[] args) {
getList(1, getSession());
getList(2, getSession());
}
private static void getList(int page, Session s) {
int rowsPerPage = 2;
String hql = "FROM User u ";
try {
Query query = s.createQuery(hql);
int start = (page - 1) * rowsPerPage;
query.setFirstResult(start);
query.setMaxResults(rowsPerPage);
//line that throws exception when int page is 2
query.list();
//line that throws exception when int page is 2
} finally {
if (s.isOpen()) {
s.close();
}
}
}
private Session getSession(){
//gets org.hibernate.Session
}
}
User Java POJO:
public class User {
private Long id;
private String username;
private String password;
private String email;
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
User table for SQL Server:
CREATE TABLE [USER] (
ID bigint NOT NULL,
USERNAME varchar(150) NOT NULL UNIQUE,
PASSWORD varchar(150) NOT NULL,
EMAIL varchar(150) NOT NULL UNIQUE,
PRIMARY KEY (ID)
);
Hibernate mapping of User POJO:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="product.model.User" table="USER">
<id column="ID" name="id" type="long">
<generator class="increment"/>
</id>
<property column="EMAIL" name="email" type="string"/>
<property column="USERNAME" name="username" type="string"/>
<property column="PASSWORD" name="password" type="string"/>
</class>
</hibernate-mapping>
Hibernate cfg xml:
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=myDB;prepareSQL=3;sendStringParametersAsUnicode=false;</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">myPassword</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.globally_quoted_identifiers">true</property>
<mapping resource="product_mapping/user.hbm.xml"/>
</session-factory>
I am using SQL Server 2012, JDK 1.8, Hibernate 4.0.1.Final, jtds driver 1.3.1
Please note that getList(1, getSession()) will not throw the exception, whereas getList(2, getSession()) will.
After hours of investigation i have find out that this problem was due to the hibernate dialect that i was using.
The correct dialect, considering the hibernate version i am required to use, is: org.hibernate.dialect.SQLServer2008Dialect
Source: https://forum.hibernate.org/viewtopic.php?p=2452163

Preventing hibernate from row duplication by column value

I have app which is storing parsed urls inside MySQL db and populating parsed sentences, then parsed Words etc. using cascade (so I just save url, db insert rest automatically).
In the ProcessedUrl POJO I have fields:
Long id
String url
Date date
Set<Sentence> sentences
For now PK is id which is generated in native way. I want to achieve something like that - when user enter some url which is already parsed and stored into db, it won't be parsed again/duplicated.
What is suitable way of achieving this using hbm.xml mapping?
EDIT:
ProcessedUrl POJO:
public class ProcessedUrl {
private long id;
private String url;
private Date date;
private Set<Sentence> sentences;
public ProcessedUrl() {
}
public ProcessedUrl(String url, Date date) {
this.setUrl(url);
this.setDate(date);
}
public ProcessedUrl(String url, Date date, Set<Sentence> sentences) {
this.setUrl(url);
this.setDate(date);
this.setSentences(sentences);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Set<Sentence> getSentences() {
return this.sentences;
}
public void setSentences(Set<Sentence> sentences) {
this.sentences = sentences;
}
#Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(!(obj instanceof ProcessedUrl)) return false;
ProcessedUrl that = (ProcessedUrl) obj;
EqualsBuilder eb = new EqualsBuilder();
eb.append(this.getUrl(), that.getUrl());
return eb.isEquals();
}
#Override
public int hashCode() {
HashCodeBuilder hcb = new HashCodeBuilder();
hcb.append(url);
return hcb.toHashCode();
}
}
ProcessedUrl.hbm.xml:
<?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 package="model">
<class name="ProcessedUrl">
<id name="id">
<column name="url_id" />
<generator class="native"/>
</id>
<property name="url" type="text"/>
<property name="date" type="java.util.Date" />
<set name="sentences" cascade="all" >
<key column="PROCESSED_URL_ID" />
<one-to-many class="model.Sentence" />
</set>
</class>
</hibernate-mapping>
When user enters some url, which is already parsed and stored into db,
it won't be parsed again/duplicated. What is suitable way of achieving
this using hbm.xml mapping ?
You can't do this with simple hbm mappings because the previously parsed URL is already stored in the database and you need to validate that the latest received URL value already exists in the database.
So, to achieve this, you need to follow the below steps:
(1) Get the URL from App (DO NOT PARSE HERE)
(2) Check the URL already exists in database
(3) If URL does NOT exist, PARSE THE URL NOW and save to database
(4) If URL already exists, log an warning/error or ignore the request

SQL Query to Hibernate ORM Query

I am trying to convert my plain SQL statements into proper Hibernate ORM ones. I've read alot about it, but still can't figure it out completely just yet. I hope some of you can help me :)
Here's some of my classes that I think relevant for this task:
WarehouseProduct (my entity class):
package exercise.java.basics.storage;
public class WarehouseProduct {
private int productID;
private String productName;
private int productCount;
public WarehouseProduct( final String productName, final int productCount ) {
this.productName = productName;
this.productCount = productCount;
}
public WarehouseProduct() {
}
public int getProductID() {
return this.productID;
}
public void setProductID( final int productID ) {
this.productID = productID;
}
public String getProductName() {
return this.productName;
}
public void setProductName( final String productName ) {
this.productName = productName;
}
public int getProductCount() {
return this.productCount;
}
public void setProductCount( final int productCount ) {
this.productCount = productCount;
}
}
my DAO.
package exercise.java.basics.storage;
import javax.transaction.Transactional;
import exercise.java.basics.storage.ProductEnum.Product;
#Transactional
public interface WarehouseDAO {
public void initializeWarehouse();
public void storeProducts( final Product product, final int count );
public void removeProducts( final Product product, final int count );
public void updateStock();
}
storeProduct() method from my DAO implementation:
public void storeProducts( final Product product, final int count ) {
//Plain-SQL, works just fine
Session session = getSessionFactory().getCurrentSession();
SQLQuery storeProductQuery = session.createSQLQuery( "UPDATE WAREHOUSE SET product_count = " + count //$NON-NLS-1$
+ " WHERE product_name = '" + product + "';" ); //$NON-NLS-1$ //$NON-NLS-2$
storeProductQuery.executeUpdate();
//Hibernate attempt, doesn't work just yet
session.get( "WarehouseProduct.class", "product_count" ); //$NON-NLS-1$ //$NON-NLS-2$
Criteria createCriteria = session.createCriteria( WarehouseProduct.class ); // Object.class = Entity
createCriteria.add( Property.forName( "product_name" ).like( product ) );
createCriteria.list();
}
When testing I don't use both (plain sql / hibernate) attempts at once of course.
I think that I am like halfway there already, but still couldn't figure out the complete hibernate approach and that's where I hope you can help me.
Basically all I need is a transformation of the plain sql string in the storeProduct() method to proper hibernate commands.
Would greatly appreciate any help you can give me.
best regards
daZza
EDIT: Here's the mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="exercise.java.basics.storage.WarehouseProduct" table="WAREHOUSE">
<id name="productID" type="integer">
<column name="product_ID" not-null="true"/>
<generator class="identity" />
</id>
<property name="productName" type="string">
<column name="product_name" length="100"/>
</property>
<property name="productCount" type="integer">
<column name="product_count"/>
</property>
</class>
</hibernate-mapping>
As to the problem, it's in the incomplete code imho. I am pretty sure that my hibernate commands are still missing something and/or are simply wrong.
This is the source SQL string I want to translate to hibernate: "UPDATE WAREHOUSE SET product_count = " + count + " WHERE product_name = '" + product + "';"
You can use the Restrictions class to get your result. use the following:
createCriteria.add(Restrictions.like("productName", product.getProductName()));
Note that I used the field name in your Product object rather than the column name in the database.Your way may also work by changing
createCriteria.add( Property.forName( "product_name" ).like( product ) );
to
createCriteria.add( Property.forName( "product_name" ).like( product.getProductName() ) );
I noticed that you were are matching "product_name" with "product" which is the entire object and not the field you are matching against.
I read even more examples and documentations and came up with the following, which I think is working now. At least there are no error messages and the hibernate logger messages look good.
Unfortunately I can't check if the data within the database is correct, as I didn't figure out how to convert hibernate query results (objects) to readable stuff like a string.
I also changed my primary key in the hibernate mapping file to be the productName instead of an auto incremented value. The reason behind that was mainly hibernates get() function, as it seems to need the PK als the second attribute. The auto generated key was kinda random so I probably would have required another select before the update to identify the PK of the row with the corresponding product. As I don't know how to convert the results of a hibernate select query that wouldn't have worked.
So here's my new class:
public void storeProducts( final Product product, final int count ) {
Session session = getSessionFactory().getCurrentSession();
WarehouseProduct productToStore = (WarehouseProduct) session.get( WarehouseProduct.class,
String.valueOf( product ) );
productToStore.setProductCount( productToStore.getProductCount() + count );
session.update( productToStore );
}

[Hibernate]Error: entity class not found:

I get tired of this for a long time. I do not know what caused this error. Here are my files:
Uzytkownik.hbm.xml
<?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="Uzytkownik" table="uzytkownicy">
<id column="id" name="id" type="int"/>
<property column="login" generated="never" lazy="false" name="login" type="string"/>
<property column="haslo" generated="never" lazy="false" name="haslo" type="string"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml:
<?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 name="">
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/sprawozdania</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="com/vaannila/uzytkownik/Uzytkownik.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I use mysql 5.5.
I get the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.vaannila.util.HibernateUtil.<clinit>(HibernateUtil.java:14)
at com.vaannila.uzytkownik.Main.saveUzyt(Main.java:22)
at com.vaannila.uzytkownik.Main.main(Main.java:16)
Caused by: org.hibernate.MappingException: entity class not found: Uzytkownik
This are my classes:
main.java
package com.vaannila.uzytkownik;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import javax.persistence.Entity;
import com.vaannila.util.HibernateUtil;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
Main obj = new Main();
String uzytkownikLogin = obj.saveUzyt("Adam", "Malysz");
}
public String saveUzyt(String login, String haslo){
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
String uzytLog = null;
try {
transaction = session.beginTransaction();
Uzytkownik uzyt = new Uzytkownik();
uzyt.setLogin(login);
uzyt.setHaslo(haslo);
uzytLog = (String) session.save(uzyt);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
return uzytLog;
}
}
Uzytkownik.java:
package com.vaannila.uzytkownik;
// default package
// Generated 2011-07-14 13:39:18 by Hibernate Tools 3.4.0.CR1
/**
* Uzytkownik generated by hbm2java
*/
public class Uzytkownik implements java.io.Serializable {
private int id;
private String login;
private String haslo;
public Uzytkownik() {
}
public Uzytkownik(int id) {
this.id = id;
}
public Uzytkownik(int id, String login, String haslo) {
this.id = id;
this.login = login;
this.haslo = haslo;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getLogin() {
return this.login;
}
public void setLogin(String login) {
this.login = login;
}
public String getHaslo() {
return this.haslo;
}
public void setHaslo(String haslo) {
this.haslo = haslo;
}
}
HibernateUtil.java:
package com.vaannila.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Maybe your mapping file is not complete but other wise it should be:
<class name="com.vaannila.uzytkownik.Uzytkownik" table="uzytkownicy">
=> need to set fully qualified class name (with package)
I think it makes sense to specify full-qualified entity class name:
<class name="com.vaannila.uzytkownik.Uzytkownik" table="uzytkownicy">
Don't forget to mention your class using as a entity classes in hibernate configeration file using the mapping tag !!
Example:
<session-factory>
//database configeration goes here
<mapping class="org.fbis.models.Form3A"/>
</session-factory>
Stijn Geukens answers right,but I want to point out more information about this question.
There are two reasons I know causing this problem: entity class not found
First, As Stijn Geukens answers, Your Hibernate mapping is not right, the value of name attribute for the tag class should be the Java class with package ahead.
Second, if you have boolean filed in your Java class, this field can't start with is.Otherwise, the hibernate throws an exception getter method is not found... when run by Java Debug mode or Run mode.But when it comes to web project and you run your project as Server Application,the message becomes entity class not found.It made me puzzled for a long time.So do not name your boolean field with is ahead.

Hibernate mapping returns null properties

I have a Hibernate mapping setup. The table is species, my Java class is Species. hibernate.cfg.xml points to mappings in species.hbn.xml
In my code I'm using a simple HQL query and then throwing the resultant Species instances into a "SpeciesLister" class (which I'm passing over to the presentation layer).
SpeciesLister speciesList = new SpeciesLister();
Query q = session.createQuery("SELECT s FROM Species s");
for (Species s : (List<Species>) q.list()){
speciesList.addSpecies(s);
}
The Species class looks like this:
package springwildlife;
public class Species implements Serializable
{
long id;
String commonName;
String latinName;
String order;
String family;
ArrayList<Sighting> sightings;
public Species()
{
}
public Species(String commonName, String latinName)
{
sightings = new ArrayList<Sighting>();
this.commonName = commonName;
this.latinName = latinName;
}
public long getId()
{
return id;
}
public String getCommonName()
{
return commonName;
}
public String getLatinName()
{
return latinName;
}
public String getOrder()
{
return order;
}
public String getFamily()
{
return family;
}
public ArrayList<Sighting> getSightings()
{
return sightings;
}
public void addSighting(Sighting s)
{
sightings.add(s);
}
public void setId(long id)
{
this.id = id;
}
public void setCommonName(String cn)
{
commonName = cn;
}
public void setLatinName(String ln)
{
commonName = ln;
}
public void setFamily(String f)
{
family = f;
}
public void setOrder(String o)
{
order = o;
}
}
My database schema looks like this:
CREATE TABLE species
(
id serial NOT NULL,
common_name text,
latin_name text,
order_name text,
family_name text,
CONSTRAINT id PRIMARY KEY (id)
)
species.hbn.xml looks like this:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="springwildlife.Species" table="species">
<id name="id" type="java.lang.Long" column="id" >
<generator class="native">
<param name="sequence">species_id_seq</param>
</generator>
</id>
<property name="commonName" type="java.lang.String">
<column name="common_name" />
</property>
<property name="latinName" type="java.lang.String">
<column name="latin_name"/>
</property>
<property name="order" type="java.lang.String">
<column name="order_name"/>
</property>
<property name="family" type="java.lang.String">
<column name="family_name"/>
</property>
</class>
</hibernate-mapping>
My SpeciesLister instance gets a full slate of all the expected number of Species instances. However, when I examine the resultant Species instances, all their fields are null except for the id (long), all the others like familyName, latinName, commonName all are null in the mapped object.
This is unexpected and I can't figure out why it is happening. Am I doing something wrong?
I'm suspicious about two things, but I'm not sure of what to make of them:
I think the fact that the id is being property set, but not the other string fields might be a clue.
I suspect something might be wrong with the way I'm casting the objects into a list of Species instances.
The code looks ok. Without getting into debugger it's hard to tell for sure, however my guess is that you have compile time class instrumentation somewhere in the build. If that's a case, I've seen cases when assignment to actual field in the class is deferred until you call getter method.
So I suggest, that you put some print statements that rely on getters to get data instead of direct access to properties and see what gets printed.
Finally, please put # sign in front of names in comments (#Mark). This way, your correspondents will get notified and you may get response sooner.

Categories