0 Replies - 13360 Views - Last Post: 05 August 2011 - 09:22 AM

#1 zakhussain299   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 25-February 11

Not able to insert elements in the database using Hibernate in Eclipse

Posted 05 August 2011 - 09:22 AM

I am not able to insert elements in the database, I am able to establish a connection, but when I try to insert elements it just says that the query is not correct. I am using Eclipse Indigo RCP, and I have Hibernate 3.0

These are my files.


hibernate.cfg.xml

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

<hibernate-configuration>
<session-factory>

 <!-- hibernate dialect -->
 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>


 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/concurrency</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password"></property>
 <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

 <!-- Automatic schema creation (begin) === -->     
 <property name="hibernate.hbm2ddl.auto">update</property>


 <!-- Simple memory-only cache -->
 <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

 <!-- Enable Hibernate's automatic session context management -->
 <property name="current_session_context_class">thread</property>

<!-- Shows the SQL in stdout -->
 <property name="show_sql">true</property>

 <!-- ############################################ -->
 <!-- # mapping files with external dependencies # -->
 <!-- ############################################ -->

 <mapping resource="Concurrency.hbm.xml"/>

</session-factory>
</hibernate-configuration>




Concurrency.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 <hibernate-mapping>

 <class name="Concurrency" table="concurrency_measure">

 <id name="Name_of_file" column="name_of_file">
 <generator class="native" />
 </id>

 <property name="No_of_processors">
 <column name="no_of_processors]" not-null="true" />
 </property>

 <property name="No_of_events">
 <column name="no_of_events" not-null="true" />
 </property>

 <property name="Concurrency">
 <column name="concurrency" not-null="true" />
 </property>

 </class>
 </hibernate-mapping>




Concurrency.java

public class Concurrency {
	String name_of_file;
	int no_of_processors;
	int no_of_events;
	float concurrency;
	
	
	/*public Concurrency(){
		
	}*/
	
	public Concurrency(String name_of_file,int no_of_processors,int no_of_events,float concurrency)
	{
		this.name_of_file=name_of_file;
		this.no_of_processors=no_of_processors;
		this.no_of_events=no_of_events;
		this.concurrency=concurrency;
	}
	
	 public String getName_of_file() {
	 return name_of_file;
	 }
	 public void setName_of_file(String name_of_file) {
	 this.name_of_file = name_of_file;
	 }
	 public int getNo_of_processors() {
	 return no_of_processors;
	 }
	 public void setNo_of_processors(int no_of_processors) {
	 this.no_of_processors = no_of_processors;
	 }
	 public int getNo_of_events() {
	 return no_of_events;
	 }
	 public void setNo_of_events(int no_of_events) {
	 this.no_of_events = no_of_events;
	 }
	 public float getConcurrency() {
	 return concurrency;
	 }
	 public void setConcurrency(float concurrency) {
	 this.concurrency = concurrency;
	 }
	}




SessionFactoryUtil.java


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class SessionFactoryUtil {

 private static final SessionFactory sessionFactory;

 static {
 try {
 // Create the SessionFactory from hibernate.cfg.xml
 sessionFactory = new Configuration().configure().buildSessionFactory();
 } catch (Throwable ex) {
 // Make sure you log the exception, as it might be swallowed
 System.err.println("Initial SessionFactory creation failed." + ex);
 throw new ExceptionInInitializerError(ex);
 }
 }

 public static SessionFactory getSessionFactory() {
 return sessionFactory;
 }
}





Cmeasure.java

public class Cmeasure {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		String s_et,e_et,size_of_comm;
		int count,event_count;
		//SessionFactoryUtil session=new SessionFactoryUtil();
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.print("Number of files to process: ");
		BufferedReader no_of_times=new BufferedReader(new InputStreamReader(System.in));
		count=Integer.parseInt(no_of_times.readLine());
		Parse_file Pfile=new Parse_file();
		String name_of_file=args[0];
		for(int i=0;i<count;i++)
		{
			Pfile.Scan(name_of_file+i);
			s_et=Pfile.val;
			e_et=Pfile.m;
			size_of_comm=Pfile.size;
			Pfile.Write_to_file(s_et,e_et,name_of_file+i);
			LSperator Sep=new LSperator(size_of_comm,e_et);
			Sep.Hasht(name_of_file+i);
			event_count=Sep.event_counter;
			InsertDB(session,name_of_file+i,size_of_comm,event_count,Sep.ConcMeasure);
		}
		/*LineChart Lchart = new LineChart("Concurrency",Sep.Individual_Conc);
        Lchart.pack();
        RefineryUtilities.centerFrameOnScreen(Lchart);
        Lchart.setVisible(true);*/
		session.getTransaction().commit();
		//session.close();
	}
	
	public static void InsertDB(Session session,String filename,String size_of_comm,int event_count,float measure)
	{
		System.out.println("The name of file is: "+filename);
		System.out.println("Number of processing elements: "+size_of_comm);
		System.out.println("Number of events: "+event_count);
		System.out.println("The concurrency measure is: "+measure);
		Concurrency conc=new Concurrency(filename,Integer.parseInt(size_of_comm),event_count,measure);
		conc.setName_of_file(filename);
		conc.setNo_of_processors(Integer.parseInt(size_of_comm));
		conc.setNo_of_events(event_count);
		conc.setConcurrency(measure);
		session.save(conc);
	}
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1