0

I am new to Hibernate, I am trying to connect it to SQL Server 2012. I am trying to apply the hello world example in this topic

http://www.java2blog.com/2013/01/hibernate-hello-world-example-in-eclipse.html

It wasn't working at the beginning, but after search in Stack Overflow, I successfully get it connected to SQL Server 2012. But I am still having an issue in mapping between my created class "User" with the database.

Here is my Configuration file

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
 <property name = "hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
 <property name = "hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
 <property name = "hibernate.connection.url">jdbc:sqlserver://KAREEM-LAPTOP;databaseName=Hibernate;instanceName=SQLEXPRESS;</property>
 <property name = "hibernate.connection.username">sa</property>
 <property name = "hibernate.connection.password">DFKI123</property>
 <property name = "hibernate.show_sql">true</property>
 <property name="hbm2ddl.auto">create</property>  
<mapping class="org.arpit.javapostsforlearning.User" table="User"></mapping>
</session-factory>
</hibernate-configuration>

The User Class

package org.arpit.javapostsforlearning;  

import javax.persistence.Column;  

import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;  


@Entity(name="User")
@Table(name = "User")
public class User {  

 @Id
 int userId;  

 @Column(name="User_Name")  
 String userName;

 @Column(name="userMessage")  
 String userMessage;  


 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;  
 }  

 public String getUserMessage() {  
  return userMessage;  
 }  

 public void setUserMessage(String userMessage) {  
  this.userMessage = userMessage;  
 }  

}

Hibernate Main Class

package org.arpit.javapostsforlearning;  

import org.hibernate.Session;  
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; 
import org.hibernate.cfg.annotations.*;
import org.hibernate.service.ServiceRegistry;  
//import org.hibernate.boot.registry.StandardServiceRegistryBuilder;


public class HibernateMain {  

 public static void main(String[] args) {  

  Configuration configuration=new Configuration(); 
  configuration.configure("hibernate.cfg.xml");  

  ServiceRegistry sr= new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();  
  SessionFactory sf=configuration.buildSessionFactory(sr);  


  User user1=new User(); 
  user1.setUserId(1);
  user1.setUserName("Arpit");  
  user1.setUserMessage("Hello world from arpit");  

  User user2=new User();  
  user2.setUserId(2);
  user2.setUserName("Ankita");  
  user2.setUserMessage("Hello world from ankita");  
  Session ss=sf.openSession();  
  ss.beginTransaction();  
 //saving objects to session  
  ss.save(user1);  
  ss.save(user2);  
  ss.getTransaction().commit();  
  ss.close();  

 }  

}

It is working fine and I got an info that Schema export complete

But after that I got an error:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.arpit.javapostsforlearning.User

I have tried to follow several topic talking about the same issue, but no one worked with me, all topics were related to annotations but I did all of their recommendations without any hope.

I tried also to add Mapping section into the configuration file as per this article Hibernate unknown entity error when saving new object

but it didn't work too,

What might cause this issue?

2
  • It's enough to use @Entity and @Table (without name) because you don't change it (it is User by default). Regarding the mapping, the following line <mapping class="org.arpit.javapostsforlearning.User" table="User"></mapping> could be changed to <mapping class="org.arpit.javapostsforlearning.User"/>. Commented Nov 11, 2015 at 10:03
  • You can try to change the User name to UserType because some words are reserved in SQL or just add a different name for @Table if you don't want to change your Java code too much. E.g.: @Table(name="USER_TABLE") Commented Nov 11, 2015 at 10:11

1 Answer 1

0

Try to change from create to update on this line:

<property name="hbm2ddl.auto">create</property> 

@Entity shouldn't have any parameter as shown below:

    @Entity
    @Table(name = "User")
    public class User {  

Also, make sure that the table name in the database is exactly the same as you declared inside annotations. I hope you can solve it this way.

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

3 Comments

I did, but the same issue still exist.
Is the table name in the database called User, or user?
"User" as the name of the class

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.