I can't retrieving the records ..?For below code.
//Client.java(Session obj,Factory obj . . .) import java.util.*; import java.io.*;
import org.hibernate.*; import org.hibernate.cfg.*; //import org.hibernate.connection.*;
public class Client {
public static void main(String[] args)throws Exception { //Anootations prog
AnnotationConfiguration cfg=new AnnotationConfiguration(); cfg.configure(); SessionFactory fact=cfg.buildSessionFactory(); Session ses=fact.openSession(); Transaction tx=ses.beginTransaction();
/*student st=new student();
st.setNo(1); st.setSname("kota"); st.setTotal(100.56);
ses.executeUpdate(st); tx.commit();*/
Query q=ses.createQuery("select count(*) from student");
List l=q.list();
for(int i=0;i<l.size();++i) { student st=(student)l.get(i); System.out.println("\n"); // System.out.println("\n"); System.out.println("\n");
System.out.println("table records is "+st.getNo()+" "+st.getSname()+" "+st.getTotal()); System.out.println("\n"); }
ses.close(); fact.close();
}
}
/* commented code to inserting the record */
//student.java(POJO class with Annotations)
import javax.persistence.*; @Entity @Table(name="student") class student { Integer no; String sname; double total; /* float avg; String result;*/
public void setNo(Integer n) { no=n; } @Id @Column(name="sno") public Integer getNo() { return no; } public void setSname(String name) { sname=name; } @Column(name="Sname") public String getSname() { return sname; } public void setTotal(double tot) { total=tot; } @Column(name="Total")
public double getTotal() { return total; } }
//hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///kittu</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property> <mapping class="student"/> </session-factory>
</hibernate-configuration>
I gotted this type of error INFO: Order SQL updates by primary key: disabled Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Order SQL inserts for batching: disabled Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory createQueryTranslatorF actory INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory Sep 25, 2010 8:09:28 AM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init> INFO: Using ASTQueryTranslatorFactory Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Query language substitutions: {} Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JPA-QL strict compliance: disabled Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Second-level cache: enabled Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Query cache: disabled Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory createCacheProvider INFO: Cache provider: org.hibernate.cache.NoCacheProvider Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Optimize cache for minimal puts: disabled Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Structured second-level cache entries: disabled Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Echoing all SQL to stdout Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Statistics: disabled Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Deleted entity synthetic identifier rollback: disabled Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Default entity-mode: pojo Sep 25, 2010 8:09:28 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Named query checking : enabled Sep 25, 2010 8:09:28 AM org.hibernate.impl.SessionFactoryImpl <init> INFO: building session factory Sep 25, 2010 8:09:28 AM org.hibernate.impl.SessionFactoryObjectFactory addInstan ce INFO: Not binding factory to JNDI, no JNDI name configured Hibernate: insert into student (Avg, Result, Sname, Total, sno) values (?, ?, ?, ?, ?) Sep 25, 2010 8:09:28 AM org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: 1062, SQLState: S1009 Sep 25, 2010 8:09:28 AM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: Invalid argument value, message from server: "Duplicate entry '10' for key 1" Sep 25, 2010 8:09:28 AM org.hibernate.event.def.AbstractFlushingEventListener pe rformExecutions SEVERE: Could not synchronize database state with session org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch updat e at org.hibernate.exception.SQLStateConverter.handledNonSpecificException (SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.j ava:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp er.java:43) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java: 253) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutio ns(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus hEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java :106) at Client.main(Client.java:114) Caused by: java.sql.BatchUpdateException: Invalid argument value, message from server: "Duplicate entry '10' for key 1" at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java: 1446) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.jav a:48) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java: 246) ... 8 more Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could n ot execute JDBC batch update at org.hibernate.exception.SQLStateConverter.handledNonSpecificException (SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.j ava:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp er.java:43) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java: 253) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutio ns(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus hEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java :106) at Client.main(Client.java:114) Caused by: java.sql.BatchUpdateException: Invalid argument value, message from server: "Duplicate entry '10' for key 1" at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java: 1446) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.jav a:48) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java: 246) ... 8 more
E:\kotapati\HB\Anno>java Client Sep 25, 2010 8:10:39 AM org.hibernate.cfg.annotations.Version <clinit> INFO: Hibernate Annotations 3.3.0.GA Sep 25, 2010 8:10:39 AM org.hibernate.cfg.Environment <clinit> INFO: Hibernate 3.2.5 Sep 25, 2010 8:10:39 AM org.hibernate.cfg.Environment <clinit> INFO: hibernate.properties not found Sep 25, 2010 8:10:39 AM org.hibernate.cfg.Environment buildBytecodeProvider INFO: Bytecode provider name : cglib Sep 25, 2010 8:10:39 AM org.hibernate.cfg.Environment <clinit> INFO: using JDK 1.4 java.sql.Timestamp handling Sep 25, 2010 8:10:39 AM org.hibernate.cfg.Configuration configure INFO: configuring from resource: /hibernate.cfg.xml Sep 25, 2010 8:10:39 AM org.hibernate.cfg.Configuration getConfigurationInputStr eam INFO: Configuration resource: /hibernate.cfg.xml Sep 25, 2010 8:10:39 AM org.hibernate.cfg.Configuration doConfigure INFO: Configured SessionFactory: null Sep 25, 2010 8:10:39 AM org.hibernate.cfg.AnnotationBinder bindClass INFO: Binding entity from annotated class: student Sep 25, 2010 8:10:39 AM org.hibernate.cfg.annotations.EntityBinder bindTable INFO: Bind entity student on table student Sep 25, 2010 8:10:39 AM org.hibernate.cfg.AnnotationConfiguration secondPassComp ile INFO: Hibernate Validator not found: ignoring Sep 25, 2010 8:10:39 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: Using Hibernate built-in connection pool (not for production use!) Sep 25, 2010 8:10:39 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: Hibernate connection pool size: 20 Sep 25, 2010 8:10:39 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: autocommit mode: false Sep 25, 2010 8:10:39 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql:///kittu Sep 25, 2010 8:10:39 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: connection properties: {user=root, password=****} Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: RDBMS: MySQL, version: 4.0.1-alpha-nt Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC driver: MySQL-AB JDBC Driver, version: 3.0.8-stable ( $Date: 2003/05/ 19 00:57:19 $, $Revision: 1.27.2.18 $ ) Sep 25, 2010 8:10:39 AM org.hibernate.dialect.Dialect <init> INFO: Using dialect: org.hibernate.dialect.MySQLDialect Sep 25, 2010 8:10:39 AM org.hibernate.transaction.TransactionFactoryFactory buil dTransactionFactory INFO: Using default transaction strategy (direct JDBC transactions) Sep 25, 2010 8:10:39 AM org.hibernate.transaction.TransactionManagerLookupFactor y getTransactionManagerLookup INFO: No TransactionManagerLookup configured (in JTA environment, use of read-wr ite or transactional second-level cache is not recommended) Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Automatic flush during beforeCompletion(): disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Automatic session close at end of transaction: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC batch size: 15 Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC batch updates for versioned data: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Scrollable result sets: enabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC3 getGeneratedKeys(): enabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Connection release mode: auto Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Maximum outer join fetch depth: 2 Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Default batch fetch size: 1 Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Generate SQL with comments: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Order SQL updates by primary key: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Order SQL inserts for batching: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory createQueryTranslatorF actory INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory Sep 25, 2010 8:10:39 AM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init> INFO: Using ASTQueryTranslatorFactory Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Query language substitutions: {} Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JPA-QL strict compliance: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Second-level cache: enabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Query cache: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory createCacheProvider INFO: Cache provider: org.hibernate.cache.NoCacheProvider Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Optimize cache for minimal puts: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Structured second-level cache entries: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Echoing all SQL to stdout Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Statistics: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Deleted entity synthetic identifier rollback: disabled Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Default entity-mode: pojo Sep 25, 2010 8:10:39 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Named query checking : enabled Sep 25, 2010 8:10:39 AM org.hibernate.impl.SessionFactoryImpl <init> INFO: building session factory Sep 25, 2010 8:10:40 AM org.hibernate.impl.SessionFactoryObjectFactory addInstan ce INFO: Not binding factory to JNDI, no JNDI name configured Hibernate: insert into student (Avg, Result, Sname, Total, sno) values (?, ?, ?, ?, ?) Sep 25, 2010 8:10:40 AM org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: tate: S1009SQL Error: 1062, SQLS Sep 25, 2010 8:10:40 AM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: Invalid argument value, message from server: "Duplicate entry '10' for key 1" Sep 25, 2010 8:10:40 AM org.hibernate.event.def.AbstractFlushingEventListener pe rformExecutions SEVERE: Could not synchronize database state with session org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch updat e at org.hibernate.exception.SQLStateConverter.handledNonSpecificException (SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.j ava:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp er.java:43) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java: 253) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutio ns(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus hEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java :106) at Client.main(Client.java:114) Caused by: java.sql.BatchUpdateException: Invalid argument value, message from server: "Duplicate entry '10' for key 1" at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java: 1446) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.jav a:48) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java: 246) ... 8 more Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could n ot execute JDBC batch update at org.hibernate.exception.SQLStateConverter.handledNonSpecificException (SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.j ava:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp er.java:43) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java: 253) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutio ns(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus hEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java :106) at Client.main(Client.java:114) Caused by: java.sql.BatchUpdateException: Invalid argument value, message from server: "Duplicate entry '10' for key 1" at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java: 1446) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.jav a:48) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java: 246) ... 8 more
E:\kotapati\HB\Anno>
|