1

I am Hibernate noob. I recently started to learn Hibernate. I am following a tutorial...but I am lost now. I have been trying to solve this problem...

hibernate.cfg.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" 
>

<hibernate-configuration>


    <session-factory>

        <property name="hibernate.dialect" >
org.hibernate.dialect.SQLServer2008Dialect
        </property>

        <property name="hibernate.connection.driver_class" >
com.microsoft.sqlserver.jdbc.SQLServerDriver
        </property>

        <property name="hibernate.connection.username" >
not required
        </property>

        <property name="hibernate.connection.password" />
        <property name="hibernate.connection.url" >
jdbc:sqlserver://localhost;databaseName=hibernate;integratedSecurity=false;
        </property>

        <property name="hibernate.cache.use_query_cache" >
true
        </property>

        <property name="hibernate.cache.region_prefix" >
hibernate.test
        </property>

        <property name="hibernate.jdbc.use_streams_for_binary" >
true
        </property>

        <property name="hibernate.jdbc.batch_size" >
0
        </property>

        <property name="hibernate.max_fetch_depth" >
3
        </property>

        <property name="hibernate.hbm2ddl.auto" >
create-drop
        </property>

        <property name="hibernate.generate_statistics" >
true
        </property>

        <property name="hibernate.cache.region.factory_class" >
org.hibernate.testing.cache.CachingRegionFactory
        </property>

        <mapping class="com.hibernate.dto.UserDetails" />



        <class-cache
            class="org.hibernate.ejb.test.item"
            usage="read-write" />

        <collection-cache
            collection="org.hibernate.ejb.test.Item.distributors"
            region="RegionName"
            usage="read-write" />

        <event type="pre-insert" />
    </session-factory>

</hibernate-configuration>

UseDetails.java

package com.hibernate.dto;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetails {

@Id
    private int userId;
    private String userName;


    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }




}

hibernateTest.java

package com.hibernate.dto;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateTest {

    public static void main(String[] args) {

        UserDetails user = new UserDetails();

        user.setUserId(1);
        user.setUserName("ahmed");



        SessionFactory sessionfact = new Configuration().configure()
                .buildSessionFactory();

        Session session = sessionfact.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        session.close();

    }

}

now when I try to run it. I get this following error every time...but I am still unsure what it is point at to? Please help me...

error log

Aug 25, 2012 10:27:40 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Aug 25, 2012 10:27:40 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.6.Final}
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Exception in thread "main" org.hibernate.MappingException: Cannot cache an unknown entity: org.hibernate.ejb
    at org.hibernate.cfg.Configuration.applyCacheConcurrencyStrategy(Configuration.java:2212)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1368)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1733)
    at com.hibernate.dto.HibernateTest.main(HibernateTest.java:25)

I looked into hibernate jar but couldn't find the org.hibernate.ejb.test.item

UPDATE

now I get these errors

Aug 25, 2012 11:51:06 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Aug 25, 2012 11:51:06 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.6.Final}
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Aug 25, 2012 11:51:06 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Aug 25, 2012 11:51:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
Aug 25, 2012 11:51:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Aug 25, 2012 11:51:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost;databaseName=hibernate;integratedSecurity=false;]
Aug 25, 2012 11:51:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=not required, password=****}
Aug 25, 2012 11:51:07 PM com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
SEVERE: Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
Aug 25, 2012 11:51:07 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
Aug 25, 2012 11:51:07 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Aug 25, 2012 11:51:07 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Aug 25, 2012 11:51:07 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.testing.cache.CachingRegionFactory]
    at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:410)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:264)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2279)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2275)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1784)
    at com.hibernate.dto.HibernateTest.main(HibernateTest.java:21)
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.testing.cache.CachingRegionFactory]
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141)
    at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:393)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.testing.cache.CachingRegionFactory
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
    ... 7 more

I added @Table (name = "hibernate") along with @Entity too.

7
  • Check this -> roseindia.net/hibernate/hibernateannotations/… I get the feeling that you have not put the annotation right. Commented Aug 25, 2012 at 18:02
  • You just have to try different possibilties and debug and keep trying till it gets solved. Not a big error Commented Aug 25, 2012 at 18:03
  • As I see, you have not defined the table name for the Class in the annotations. Commented Aug 25, 2012 at 18:04
  • @anuj I thing I am not using annotations Commented Aug 25, 2012 at 18:22
  • @ Entity and @ Id is an annotation; Commented Aug 25, 2012 at 18:38

1 Answer 1

1

I suspect the missing class is part of the tutorial you are following. To keep things simple, comment out the class-cache and collection-cache elements in your XML configuration and set the use_query_cache property to false. Also comment out any other properties relating to caching.

Given updated error log, remove hibernate.cache.region.factory_ class property from XML config. Like I said before, get your code working without caching configuration first.

Sign up to request clarification or add additional context in comments.

6 Comments

Commenting made thing more complicated. Now there are three errors :( Exception in thread "main" org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.testing.cache.CachingRegionFactory]
You still have caching properties defined. I have edited my answer once again.
Now I commented all the cache- related things..than came sqljdbc4.jar error...I downloaded the sqljdbc4...added the jars...but the problem of sqljbd4.jar is still there...
Aug 26, 2012 12:29:16 AM com.microsoft.sqlserver.jdbc.SQLServerConnection <init> SEVERE: Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0. Aug 26, 2012 12:29:16 AM org.hibernate.tool.hbm2ddl.SchemaExport execute ERROR: HHH000231: Schema export unsuccessful java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
Looks like you need to find a version of that JDBC driver which supports Java 7 or switch and use Java 6.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.