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 + "';"