Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.58 KB | None | 0 0
  1. CREATE TABLE student(
  2. id int(3) NOT NULL,
  3. firstName varchar(20) NOT NULL,
  4. age int(2) NOT NULL,
  5. CONSTRAINT id_pk PRIMARY KEY (id)
  6. );
  7.  
  8. INSERT INTO student VALUES ('101','yashik','23');
  9.  
  10. SELECT * FROM student;
  11.  
  12. package com.demo.entity;
  13. import javax.persistence.Entity;
  14. import javax.persistence.Id;
  15. import javax.persistence.Table;
  16.  
  17. @Entity
  18. @Table(name="student")
  19. public class StudentEntity {
  20. @Id
  21. private int id;
  22. private String firstName;
  23. private int age;
  24. //getter and setter
  25.  
  26. package com.demo.dao;
  27. import java.util.List;
  28. import org.hibernate.Session;
  29. import org.hibernate.SessionFactory;
  30. import org.hibernate.query.Query;
  31. import com.demo.entity.StudentEntity;
  32. import com.demo.resources.HibernateUtility;
  33. public class StudentDAOImpl implements StudentDAO {
  34. @Override
  35. public StudentEntity getStudent(Integer studentID) {
  36. SessionFactory sessionFactory=HibernateUtility.createSessionFactory();
  37. Session s1=null;
  38. StudentEntity stu=null;
  39. s1=sessionFactory.openSession();
  40. s1.beginTransaction();
  41. String st1="SELECT s FROM StudentEntity s";
  42. Query q1=s1.createQuery(st1);
  43. List<StudentEntity> l1=q1.list();
  44. stu.setAge(l1.get(0).getAge());
  45. stu.setId(l1.get(0).getId());
  46. stu.setFirstName(l1.get(0).getFirstName());
  47. if (s1 != null) {
  48. s1.close();
  49. }
  50. return stu;
  51. }
  52. }
  53.  
  54. package com.demo.resources;
  55.  
  56. import org.hibernate.SessionFactory;
  57. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  58. import org.hibernate.cfg.Configuration;
  59. import org.hibernate.service.ServiceRegistry;
  60.  
  61. public class HibernateUtility {
  62.  
  63. private static final String CONFIGURATION_LOCATION="com/demo/resources/hibernate.cfg.xml";
  64. private static SessionFactory sessionFactory=getSessionFactory();
  65.  
  66. public static SessionFactory getSessionFactory() {
  67. if (sessionFactory == null) {
  68. // loads configuration and mappings
  69. Configuration configuration = new Configuration().configure(CONFIGURATION_LOCATION);
  70. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  71. .applySettings(configuration.getProperties()).build();
  72.  
  73. // builds a session factory from the service registry
  74. sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  75. }
  76.  
  77. return sessionFactory;
  78. }
  79. public static SessionFactory createSessionFactory(){
  80. return getSessionFactory();
  81. }
  82. public static void closeSessionFactory(){
  83. if(!sessionFactory.isClosed()){
  84. sessionFactory.close();
  85. }
  86. }
  87. }
  88.  
  89. <?xml version="1.0" encoding="utf-8"?>
  90. <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  91.  
  92. <hibernate-configuration>
  93. <session-factory>
  94. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  95. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  96. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/rmc</property>
  97. <property name="hibernate.connection.username">root</property>
  98. <property name="hibernate.connection.password">******</property>
  99. <property name="show_sql">true</property>
  100. <property name="connection.pool_size">1</property>
  101.  
  102. <mapping class="com.demo.entity.StudentEntity"></mapping>
  103. </session-factory>
  104. </hibernate-configuration>
  105.  
  106. Jul 06, 2016 10:06:18 PM org.hibernate.Version logVersion
  107. INFO: HHH000412: Hibernate Core {5.2.0.Final}
  108. Jul 06, 2016 10:06:18 PM org.hibernate.cfg.Environment <clinit>
  109. INFO: HHH000206: hibernate.properties not found
  110. Jul 06, 2016 10:06:18 PM org.hibernate.cfg.Environment buildBytecodeProvider
  111. INFO: HHH000021: Bytecode provider name : javassist
  112. Jul 06, 2016 10:06:19 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
  113. INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
  114. Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  115. WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
  116. Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  117. INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/rmc]
  118. Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  119. INFO: HHH10001001: Connection properties: {user=root, password=****}
  120. Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  121. INFO: HHH10001003: Autocommit mode: false
  122. Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
  123. INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
  124. Wed Jul 06 22:06:19 IST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
  125. Jul 06, 2016 10:06:19 PM org.hibernate.dialect.Dialect <init>
  126. INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
  127. Jul 06, 2016 10:06:19 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
  128. INFO: HHH000397: Using ASTQueryTranslatorFactory
  129. Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: StudentEntity is not mapped [SELECT s FROM StudentEntity s]
  130. at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:131)
  131. at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
  132. at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
  133. at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:633)
  134. at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:101)
  135. at com.demo.dao.StudentDAOImpl.getStudent(StudentDAOImpl.java:22)
  136. at com.demo.userInterface.UserInterface.main(UserInterface.java:9)
  137. Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: StudentEntity is not mapped [SELECT s FROM StudentEntity s]
  138. at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
  139. at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
  140. at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
  141. at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
  142. at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
  143. at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
  144. at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:152)
  145. at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:523)
  146. at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:625)
  147. ... 3 more
  148. Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: StudentEntity is not mapped
  149. at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
  150. at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
  151. at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
  152. at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:321)
  153. at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3704)
  154. at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3593)
  155. at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:718)
  156. at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:574)
  157. at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:311)
  158. at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:259)
  159. at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
  160. at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
  161. ... 9 more
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement