Friday, 5 December 2014

Hibernate Pojo Annotations

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.petrol.to;

// Generated Mar 8, 2014 3:29:01 AM by Hibernate Tools 3.4.0.CR1

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Bills generated by hbm2java
 */
@Entity
@Table(name = "bills", catalog = "pt")
public class Bills implements java.io.Serializable {

 private Integer id;
 private Customers customers;
 private int billNo;
 private Integer vehicleId;
 private Date billDate;
 private int type;
 private Date createdAt;
 private Date updatedAt;
 private Set<BillItems> billItemses = new HashSet<BillItems>(0);

 public Bills() {
 }

 public Bills(int billNo, int type) {
  this.billNo = billNo;
  this.type = type;
 }

 public Bills(Customers customers, int billNo, Integer vehicleId,
   Date billDate, int type, Date createdAt, Date updatedAt,
   Set<BillItems> billItemses) {
  this.customers = customers;
  this.billNo = billNo;
  this.vehicleId = vehicleId;
  this.billDate = billDate;
  this.type = type;
  this.createdAt = createdAt;
  this.updatedAt = updatedAt;
  this.billItemses = billItemses;
 }

 @Id
 @GeneratedValue(strategy = IDENTITY)
 @Column(name = "id", unique = true, nullable = false)
 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 @ManyToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "customer_id")
 public Customers getCustomers() {
  return this.customers;
 }

 public void setCustomers(Customers customers) {
  this.customers = customers;
 }

 @Column(name = "bill_no", nullable = false)
 public int getBillNo() {
  return this.billNo;
 }

 public void setBillNo(int billNo) {
  this.billNo = billNo;
 }

 @Column(name = "vehicle_id")
 public Integer getVehicleId() {
  return this.vehicleId;
 }

 public void setVehicleId(Integer vehicleId) {
  this.vehicleId = vehicleId;
 }

 @Temporal(TemporalType.DATE)
 @Column(name = "bill_date", length = 10)
 public Date getBillDate() {
  return this.billDate;
 }

 public void setBillDate(Date billDate) {
  this.billDate = billDate;
 }

 @Column(name = "type", nullable = false)
 public int getType() {
  return this.type;
 }

 public void setType(int type) {
  this.type = type;
 }

 @Temporal(TemporalType.TIMESTAMP)
 @Column(name = "created_at", length = 19)
 public Date getCreatedAt() {
  return this.createdAt;
 }

 public void setCreatedAt(Date createdAt) {
  this.createdAt = createdAt;
 }

 @Temporal(TemporalType.TIMESTAMP)
 @Column(name = "updated_at", length = 19)
 public Date getUpdatedAt() {
  return this.updatedAt;
 }

 public void setUpdatedAt(Date updatedAt) {
  this.updatedAt = updatedAt;
 }

 @OneToMany(fetch = FetchType.EAGER, mappedBy = "bills")
 public Set<BillItems> getBillItemses() {
  return this.billItemses;
 }

 public void setBillItemses(Set<BillItems> billItemses) {
  this.billItemses = billItemses;
 }

}


hibernate.cfg.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/pt</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <mapping class="com.petrol.to.Customers" />
        <mapping class="com.petrol.to.Invoices" />
        <mapping class="com.petrol.to.Authorizations" />
        <mapping class="com.petrol.to.Sessions" />
        <mapping class="com.petrol.to.PetrolUser" />
        <mapping class="com.petrol.to.Stocks" />
        <mapping class="com.petrol.to.Actions" />
        <mapping class="com.petrol.to.SchemaMigrations" />
        <mapping class="com.petrol.to.Vehicles" />
        <mapping class="com.petrol.to.UserType" />
        <mapping class="com.petrol.to.Items" />
        <mapping class="com.petrol.to.BillItems" />
        <mapping class="com.petrol.to.Bills" />
        <mapping class="com.petrol.to.Recipts" />
    </session-factory>
</hibernate-configuration>


main.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.petrol.admin.action;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.petrol.dao.AuthorizationsHome;
import com.petrol.to.Authorizations;

/**
 * Handle authorization operation.
 * 
 * @author Asman.
 */
public class AuthorizationsLoader {

 /**
  * Getting authorizations.
  * 
  * @return authorizations
  */
 public List<Authorizations> loadAuthorizations() {
  List<Authorizations> authorizations = null;
  Session session = null;
  try {
   SessionFactory sessionFactory = new AnnotationConfiguration()
     .configure("/hibernate.cfg.xml").buildSessionFactory();
   session = sessionFactory.openSession();
   AuthorizationsHome authoDao = new AuthorizationsHome();
   authorizations = authoDao.getAllAuthorizations(session);
   session.close();

  } catch (HibernateException he) {
   // he.printStackTrace();
  }
  return authorizations;
 }

 public static void main(String s[]) {

 }
}

No comments:

Post a Comment