Guest User

Untitled

a guest
Dec 29th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped [from users]
  2. org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
  3. org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
  4. org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
  5.  
  6. public List<User> getUsers() {
  7. Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  8. session.beginTransaction();
  9. List<User> result = (List<User>) session.createQuery("from users").list();
  10. session.getTransaction().commit();
  11. return result;
  12. }
  13.  
  14. public void addUser(User user) {
  15. Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  16. session.beginTransaction();
  17. session.save(user);
  18. session.getTransaction().commit();
  19. }
  20.  
  21. public void addUser(List<User> users) {
  22. Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  23. session.beginTransaction();
  24. for (User user : users) {
  25. session.save(user);
  26. }
  27. session.getTransaction().commit();
  28. }
  29.  
  30. <hibernate-configuration>
  31. <session-factory>
  32. <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
  33. <property name="connection.username">root</property>
  34. <property name="connection.password">root</property>
  35. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  36. <property name="hibernate.default_schema">test</property>
  37. <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
  38.  
  39. <property name="show_sql">true</property>
  40.  
  41. <property name="format_sql">true</property>
  42. <property name="hbm2ddl.auto">create-drop</property>
  43.  
  44. <!-- JDBC connection pool (use the built-in) -->
  45. <property name="connection.pool_size">1</property>
  46. <property name="current_session_context_class">thread</property>
  47.  
  48. <!-- Mapping files will go here.... -->
  49.  
  50. <mapping class="model.Company" />
  51. <mapping class="model.Conference" />
  52. <mapping class="model.ConferencesParticipants" />
  53. <mapping class="model.ConferenceParticipantStatus" />
  54. <mapping class="model.ConferencesUsers" />
  55. <mapping class="model.Location" />
  56. <mapping class="model.User" />
  57.  
  58. </session-factory>
  59.  
  60. @Entity
  61. @Table( name = "Users" )
  62. public class User implements Serializable{
  63.  
  64. private long userID;
  65. private int pasportID;
  66. private Company company;
  67. private String name;
  68. private String email;
  69. private String phone1;
  70. private String phone2;
  71. private String password; //may be null/empty , will be kept hashed
  72. private boolean isAdmin;
  73. private Date lastLogin;
  74.  
  75. User() {} //not public on purpose!
  76.  
  77. public User(int countryID, Company company, String name, String email,
  78. String phone1, String phone2, String password, boolean isAdmin) {
  79. this.pasportID = countryID;
  80. this.company = company;
  81. this.name = name;
  82. this.email = email;
  83. this.phone1 = phone1;
  84. this.phone2 = phone2;
  85. this.password = password;
  86. this.isAdmin = isAdmin;
  87. }
  88.  
  89. @Id
  90. @GeneratedValue(generator="increment")
  91. @GenericGenerator(name="increment", strategy = "increment")
  92. public long getUserID() {
  93. return userID;
  94. }
  95. public void setUserID(long userID) {
  96. this.userID = userID;
  97. }
  98. ...
  99. }
  100.  
  101. List<User> result = (List<User>) session.createQuery("from User").list();
  102.  
  103. Query query = entityManager. createQuery("Select UserName from **UserDetails** ");
  104.  
  105. import org.hibernate.annotations.Entity;
  106.  
  107. import javax.persistence.Entity;
  108.  
  109. import javax.persistence.*;
  110.  
  111. @Entity
  112. @Table(name = "VENDOR")
  113. public class Vendor {
  114.  
  115. //~ --- [INSTANCE FIELDS] ------------------------------------------------------------------------------------------
  116. private int id;
  117. private String name;
  118.  
  119. //~ --- [METHODS] --------------------------------------------------------------------------------------------------
  120. @Override
  121. public boolean equals(final Object o) {
  122. if (this == o) {
  123. return true;
  124. }
  125.  
  126. if (o == null || getClass() != o.getClass()) {
  127. return false;
  128. }
  129.  
  130. final Vendor vendor = (Vendor) o;
  131.  
  132. if (id != vendor.id) {
  133. return false;
  134. }
  135.  
  136. if (name != null ? !name.equals(vendor.name) : vendor.name != null) {
  137. return false;
  138. }
  139.  
  140. return true;
  141. }
  142.  
  143. //~ ----------------------------------------------------------------------------------------------------------------
  144. @Column(name = "ID")
  145. @GeneratedValue(strategy = GenerationType.AUTO)
  146. @Id
  147. public int getId() {
  148. return id;
  149. }
  150.  
  151. @Basic
  152. @Column(name = "NAME")
  153. public String getName() {
  154.  
  155. return name;
  156. }
  157.  
  158. public void setId(final int id) {
  159. this.id = id;
  160. }
  161.  
  162. public void setName(final String name) {
  163. this.name = name;
  164. }
  165.  
  166. @Override
  167. public int hashCode() {
  168. int result = id;
  169. result = 31 * result + (name != null ? name.hashCode() : 0);
  170. return result;
  171. }
  172. }
  173.  
  174. <property name="packagesToScan" value="yourpackage" />
  175.  
  176. @Query(value = 'select ID, CLUMN2, CLUMN3 FROM VENDOR c where c.ID = :ID', nativeQuery = true)
  177.  
  178. @javax.persistence.Entity
  179. @javax.persistence.Table(name = "Users")
  180. public class User {
  181.  
  182. // this sets the name of the table and the name of the entity
  183. @javax.persistence.Entity(name = "Users")
  184. public class User implements Serializable{
  185.  
  186. private static SessionFactory buildsSessionFactory() {
  187. try {
  188. if (sessionFactory == null) {
  189. StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
  190. .configure("/hibernate.cfg.xml").build();
  191. Metadata metaData = new MetadataSources(standardRegistry)
  192. .getMetadataBuilder().build();
  193. sessionFactory = metaData.getSessionFactoryBuilder().build();
  194. }
  195. return sessionFactory;
  196. } catch (Throwable th) {
  197.  
  198. System.err.println("Enitial SessionFactory creation failed" + th);
  199.  
  200. throw new ExceptionInInitializerError(th);
  201.  
  202. }
  203. }
Add Comment
Please, Sign In to add comment