Posts Tagged Hibernate

java.lang.ClassNotFoundException: org.apache.commons.collections.map.LRUMap


Problem: Another issue, this time with JUnit 4 and Spring3, 
Hibernate application, where in i encountered the following  
issue that 
java.lang.ClassNotFoundException: 
org.apache.commons.collections.map.LRUMap 

Solution:  Add the following dependency to the pom.xml to resolve 
java.lang.ClassNotFoundException: 
org.apache.commons.collections.map.LRUMap 

<dependency> 
<groupId>commons-collections</groupId>
 <artifactId>commons-collections</artifactId>
 <version>3.1</version> </dependency> 

, ,

1 Comment

package org.springframework.orm.hibernate3 does not exist


Problem: Recently i am working on SpringMVC REST +Hibernate application, where in i encountered the following 
issue during the integration package org.springframework.orm.hibernate3 does  not exist 

 Solution:  Add the following dependency to the pom.xml

 <dependency>  
<groupId>org.springframework</groupId>
 <artifactId>spring-orm</artifactId>   
<version>${org.springframework.version}</version> </dependency> 

Replace the above version with the actual version or specify the version as a property in pom.xml.  
Note that, when i was working on Spring 2.5 + Hibernate, the above dependency was not needed, 
 but with Spring 3 and Hibernate, to resolve the issue that package org.springframework.orm.hibernate3
 does not exist, i added the above dependency 

, ,

Leave a comment

Hibernate Annotations Named native Query


Problem: How to use named native queries in Hibernate/JPA Annotated Entities

Solution:

In the entity class, you can use these below annotations. Continuing with the earlier post, regarding the oracle connect sql, if chosen to

use the sql from an entity will look like below

@Entity
@SqlResultSetMapping(name = "implicit", entities = @EntityResult(entityClass = pakagepath.Employee.class))
@NamedNativeQuery(name = "namednativeqry", query = "select * from customer start with manager_id = :mgrId connect by prior emp_id = manager_id D relationship_cd = ‘IS MANAGER OF’", resultSetMapping = "implicit")

From the code, this can be called using the 

session.getNamedQuery("nativenamedqry").setString("mgrId",334);

,

Leave a comment

Mapping List Object in Hibernate


Below self explanatory sample application helps to understand how to persist a list object using Hibernate as persistence layer and MySQL as database.

—————————————————————————————————

package project2;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.Transaction;

import project2.persistence.HibernateUtil;
public class MappingListDemo {

public static void main(String[] args) {

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();

try {
customers cust1 = new customers(“Dave”);

ArrayList list1 = new ArrayList();
list1.add(new custorders(“SOA Using Java Web Services”));
list1.add(new custorders(“Java Persistence with Hibernate”));
cust1.setOrders(list1);
session.save(cust1);

customers cust2 = new customers(“Jim”);
ArrayList list2 = new ArrayList();
list2.add(new custorders(“Java Unleashed”));
list2.add(new custorders(“Design Patterns”));
cust2.setOrders(list2);
session.save(cust2);

tx.commit();

session.flush();
session.close();
} catch (Exception he)

{
System.out.println(“Exception caught: ” + he);
}
}

}

————————————————————————————————–

————————————————————————————————–

package project2;
public class custorders {
String orderinfo;

int id;
public custorders() {
}
public custorders(String d) {
orderinfo = d;
}
public int getId() {
return id;
}

public void setId(int s) {
id = s;
}
public void setOrderinfo(String orderinfo) {
this.orderinfo = orderinfo;
}

public String getOrderinfo() {
return orderinfo;
}
}

————————————————————————————————–

—————————————————————————————————

package project2;
import java.util.List;

public class customers {
String name;
List orders;
int id;

public customers() {
}

public customers(String d) {
name = d;
}
public String getName() {
return name;
}

public void setName(String a) {
name = a;
}
public int getId() {
return id;
}

public void setId(int s) {
id = s;
}
public void setOrders(List orders) {
this.orders = orders;
}

public List getOrders() {
return orders;
}
}

————————————————————————————————–

—————————————————————————————————

<?xml version=”1.0″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD//EN”
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“>

<hibernate-mapping>

<class name=”project2.customers”   table=”customer”>
<id name=”id” >

<generator/>

</id>

<property name=”name” />

<list name=”orders” cascade=”all”>

<key column=”item_id” />
<list-index column=”position” />
<one-to-many />

</list>

</class>

</hibernate-mapping>

————————————————————————————————–

—————————————————————————————————

<?xml version=”1.0″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD//EN”
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“>

<hibernate-mapping>

<class name=”project2.custorders”   table=”custorder”>
<id name=”id”>

<generator/>

</id>

<property name=”orderinfo”  />

</class>

</hibernate-mapping>

————————————————————————————————–

—————————————————————————————————
<!DOCTYPE hibernate-configuration SYSTEM
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd“>
<hibernate-configuration>
<session-factory>
<property name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/hibernatetest</property>
<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.connection.password”></property>
<property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>
<!– Use the C3P0 connection pool provider –>
<property name=”hibernate.c3p0.min_size”>5</property>
<property name=”hibernate.c3p0.max_size”>20</property>
<property name=”hibernate.c3p0.timeout”>300</property>
<property name=”hibernate.c3p0.max_statements”>50</property>
<property name=”hibernate.c3p0.idle_test_period”>3000</property>
<!– Show and print nice SQL on stdout –>
<property name=”show_sql”>true</property>
<property name=”format_sql”>true</property>
<!– List of XML mapping files –>

<mapping/>
<mapping/>

<mapping resource=”project2/custorders.hbm.xml”/>
<mapping resource=”project2/customers.hbm.xml”/>
</session-factory>
</hibernate-configuration>

—————————————————————————————————

MySQL query tables used in above examples…

create table custorder(id int not null primary key auto_increment,
orderinfo text,position int,item_id int);

create table customer
( id int not null primary key auto_increment, name VARCHAR(40));

,

Leave a comment