Link to home
Create AccountLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

hibernate populate table data

Hi,

I am able to populate using hibernate test client program into userdetails table. Similar way i am trying to populate into user_details table. I am getting errors.



hibernate config file looks like below

<?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.connection.driver_class">org.postgresql.Driver</property>
  <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
  <property name="hibernate.connection.username">postgres</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibersnate.connection.pool_size">10</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  <property name="hibernate.hbm2ddl.auto">create</property> 
  <mapping class="org.gp.gpr.dto.UserDetails"/>  
   <mapping class="org.gp.gpr.dto.Vehicle"/> 
   <mapping class="org.gp.gpr.dto.TwoWheeler"/> 
   <mapping class="org.gp.gpr.dto.FourWheeler"/> 
   </session-factory>
</hibernate-configuration>



hibernate test client hjava looks below

package org.gp.gpr.hibernate;

import java.util.Date;

import org.gp.gpr.dto.Address;
import org.gp.gpr.dto.FourWheeler;
import org.gp.gpr.dto.TwoWheeler;
import org.gp.gpr.dto.UserDetails;
import org.gp.gpr.dto.Vehicle;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateTestInheritance {

public static void main(String[] args){

	SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
	Session session=sessionFactory.openSession();
	session.beginTransaction();
	
	//UserDetails exampleUser=new UserDetails();
  
	for(int i=0;i<12;i++){
		UserDetails user=new UserDetails();
	user.setUserName("User"+i);
	session.save(user);

	}
//UserDetails user=	(UserDetails) session.get(UserDetails.class, 5);
//user.setUserName("updateddd user");
//session.update(user);
	//session.delete(user);
session.getTransaction().commit();
session.close();
//System.out.println("user name puled is"+user.getUserName());

	
}
}



UserObjects class i modified as below

package org.gp.gpr.dto;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
//import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NamedNativeQuery;
import org.hibernate.annotations.Type;

//import org.hibernate.annotations.Entity;

@Entity
@NamedQuery(name="User_Details.byId", query="from User_Details where userId=?")
//@NamedNativeQuery(name="UserDetails.byName", query="select * from User_Details where username=?", resultClass=UserDetails.class)
@org.hibernate.annotations.Entity(selectBeforeUpdate=true)
public class UserDetails {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
private String userName;

public int getUserId() {
	return userId;
}
public void setUserId(int userId) {
	this.userId = userId;
}

public String getUserName() {
	return userName+"from get";
}


public void setUserName(String userName) {
	this.userName = userName;
}




}


basically i changed below line
@NamedQuery(name="User_Details.byId", query="from User_Details where userId=?")

earlier it was
@NamedQuery(name="UserDetails.byId", query="from UserDetails where userId=?")

Open in new window


When i run getting below error
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.HibernateException: Errors in named queries: User_Details.byId
      at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:426)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
      at org.gp.gpr.hibernate.HibernateTestInheritance.main(HibernateTestInheritance.java:18)


how can i fix it.
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
Table2.jpg
Table3.jpg
Table4Still-Empty.jpg
ASKER CERTIFIED SOLUTION
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account