Monday, February 28, 2011

I hate Java #2: ClassNotFoundException ?! – Tomcat deployment

At least once per day I have this problem with Tomcat and Eclipse.

I have a library declared in my pom.xml but Tomcat strangely enough doesn’t deploy it, and at deploy I have this ClassNotFoundException, the problem is that sometimes when the stars are not arranged in one line Tomcat doesn’t deploy all the libraries into its webapps/{PROJECT_NAME} (deployment folder) folder.


Why ? I don’t know but I know instead how to solve it. First of all stop the server, remove all the apps from it. 


Click “Finish”.


Make a Tomcat clean.


After the clean is finished add  removed apps back to Tomcat and run the server.

As you can see I don’t have the problem now.


Another one why I hate Java, and yes it isn’t related to Java itself but to it environment, but still I don’t fee much happier about it. This shouldn’t be like this, guys. Dear clients, take a look at IIS. Do you see something like that ? No ? Than why do I still have to choose Java ?

Thursday, February 17, 2011

I hate Java #1: org.hibernate.MappingException: Unknown entity

Well, as you might know I started learning/coding Java because I will kinda be working in production and as you might know in my first month in Java I got the idea to write a book about it. Here is how the cover would look.


Anyway this is not the point of this particular post. So let’s see what we got. We got a very unusual exception: org.hibernate.MappingException: Unknown entity.

We tried to use annotated hibernate entities, everything seemed to be nice configured in hibernate.xml but it didn’t worked. Our Hibernate should work with Spring. So I’ll put some XML’s and classes before and than I’ll tell you the solution

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:tx=""

    <!-- Hibernate session factory -->

    <bean id="dataSource"
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/testh" />
        <property name="username" value="postgres" />
        <property name="password" value="postgres" />

    <bean id="sessionFactory"

        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="entity" />
        <property name="hibernateProperties">
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="">update</prop>

    <tx:annotation-driven />

    <bean id="transactionManager"
        <property name="sessionFactory" ref="sessionFactory" />

    <!-- The Spring support for Hibernate calls -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />


The property “packagesToScan” is used to indicate where the hibernate annotation entities are.

Now the entity java class was like this

package entity;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Entity;

@Table(name = "emp", schema = "public")
public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "emp_userid")
    private String id;

    @Column(name = "emp_link_date")
    private Timestamp date;

    @Column(name = "emp_logdel")
    private boolean logicalDelete;

    @Column(name = "emp_name")
    private String name;

    @Column(name = "employee_surname")
    private String surname;

    public Employee() {
        // TODO Auto-generated constructor stub

    public String getId() {
        return id;

    public void setId(String id) { = id;

    public Timestamp getDate() {
        return date;

    public void setDate(Timestamp date) { = date;

    public boolean getLogicalDelete() {
        return logicalDelete;

    public void setLogicalDelete(boolean logicalDelete) {
        this.logicalDelete = logicalDelete;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getSurname() {
        return surname;

    public void setSurname(String surname) {
        this.surname = surname;

    public boolean isLogicalDelete() {
        return logicalDelete;


Well I got this error which was kind of stupid:

org.hibernate.MappingException: Unknown entity: entity.Employee
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(
    at org.hibernate.event.def.DefaultLoadEventListener.onLoad(
    at org.hibernate.impl.SessionImpl.fireLoad(
    at org.hibernate.impl.SessionImpl.get(
    at org.hibernate.impl.SessionImpl.get(
    at com.givaudan.persistence.UserDaoBean.getUserById(
    at com.givaudan.masterdata.service.UserRightsServiceBean.getUserById(
    at service.UserRightsServiceTest.testGetUserById(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at org.springframework.test.context.junit4.SpringTestMethod.invoke(
    at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(
    at org.springframework.test.context.junit4.SpringMethodRoadie$
    at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(
    at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(
    at org.junit.internal.runners.JUnit4ClassRunner$
    at org.junit.internal.runners.ClassRoadie.runUnprotected(
    at org.junit.internal.runners.ClassRoadie.runProtected(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(

And I got to this blog post on the net.

Now that explained everything: I shoudn’t use org.hibernate.annotations.Entity, but  javax.persistence.Entity.

Isn’t that sweet ? For a newbie in Java it’s really hard to understand why Entity annotation which you do use at hibernate, shouldn’t be in org.hibernate.annotations.Entity, but instead in javax.persistence.Entity.

Well looks like java API coders don’t like logical and simple things. And another question when I should use org.hibernate.annotations.Entity? Do I really need it with the same name in this package?

Well may be someone on the Java side would explain this thing. That really would be nice.