Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. @Entity
  2. @Table(name = "dbuser", schema = "public", catalog = "postgres")
  3. public class DbuserEntity {
  4. private int id;
  5. private String username;
  6. private String createdBy;
  7. private Date createdDate;
  8. private String firstName;
  9. private String lastName;
  10.  
  11. @OrderBy("firstName")
  12. public List<DbuserEntity> dbusers = new ArrayList<DbuserEntity>();
  13.  
  14. //constructors
  15.  
  16. @Id
  17. @SequenceGenerator(name="pk_sequence",sequenceName="entity_id_seq", allocationSize=1)
  18. @GeneratedValue(strategy=GenerationType.AUTO,generator="pk_sequence")
  19. @Column(name = "id")
  20. public int getId() {
  21. return id;
  22. }
  23.  
  24. public void setId(int id) {
  25. this.id = id;
  26. }
  27.  
  28. @Basic
  29. @Column(name = "username")
  30. public String getUsername() {
  31. return username;
  32. }
  33.  
  34. public void setUsername(String username) {
  35. this.username = username;
  36. }
  37.  
  38. // other getters and setters, equals etc.
  39.  
  40. public List readAll() {
  41. HibernateUtils hibernateUtils = new HibernateUtils();
  42. Session session = null;
  43. List users = new ArrayList<DbuserEntity>();
  44. try {
  45. session = hibernateUtils.getSessionFactory().openSession();
  46. session.beginTransaction();
  47. users = session.createQuery("FROM DbuserEntity ").list();
  48.  
  49. session.beginTransaction().commit();
  50. } catch (HibernateException e) {
  51. session.beginTransaction().rollback();
  52. e.printStackTrace();
  53. } finally {
  54. if (session != null) {
  55. if (session.isOpen()) {
  56. session.close();
  57. }
  58. }
  59. }
  60. return users;
  61. }
  62.  
  63. public static void main(String[] args) {
  64. Service service = new Service();
  65. DbuserEntity db = new DbuserEntity();
  66. List users = new ArrayList<DbuserEntity>();
  67. users = service.readAll();
  68. db.dbusers = users;
  69. for (DbuserEntity dbuser : db.dbusers) {
  70. System.out.println(dbuser.toString());
  71. }
  72.  
  73. }
  74. }
  75.  
  76. DbuserEntity{id=19, username='user2', createdby='admin', createddate=2014-11-19, firstname='john', lastname='johnson'}
  77.  
  78. DbuserEntity{id=20, username='user3', createdby='admin', createddate=2014-11-19, firstname='ivan', lastname='ivanov'}
  79.  
  80. DbuserEntity{id=26, username='newUSer', createdby='system', createddate=2014-11-21, firstname='AAA', lastname='BBB'}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement