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[]) {

 }
}

Struts2 Restful webservices


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <constant name="struts.devMode" value="true" />
  <constant name="struts.convention.action.suffix" value="Controller"/>
  <constant name="struts.convention.action.mapAllMatches" value="true"/>
  <constant name="struts.convention.default.parent.package" value="rest-default"/>
  <constant name="struts.convention.package.locators" value="example"/>

  <constant name="struts.convention.default.parent.package" value="rest-default"/>  

</struts>

OrdersController.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
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
package org.lkr.struts2.rest.example;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.lkr.struts2.rest.service.OrdersService;
import org.lkr.struts2.rest.to.Order;

import com.opensymphony.xwork2.ModelDriven;

@Results({ @Result(name = "success", type = "redirectAction", params = {
  "actionName", "orders" }) })
public class OrdersController implements ModelDriven<Object> {

 /**
  * 
  */
 private static final long serialVersionUID = 3241256322152936000L;
 private Order model = new Order();
 private String id;
 private Collection<Order> list;
 private OrdersService ordersService = new OrdersService();

 // GET /orders/1
 public HttpHeaders show() {
  return new DefaultHttpHeaders("show");
 }

 // GET /orders
 public HttpHeaders index() {
  list = ordersService.getAll();
  return new DefaultHttpHeaders("index").disableCaching();
 }

 // GET /orders/1/edit
 public String edit() {
  return "edit";
 }

 // GET /orders/new
 public String editNew() {
  model = new Order();
  return "editNew";
 }

 // GET /orders/1/deleteConfirm
 public String deleteConfirm() {
  return "deleteConfirm";
 }

 // DELETE /orders/1
 public String destroy() {
  ordersService.remove(id);
  //addActionMessage("Order removed successfully");
  return "success";
 }

 // POST /orders
 public HttpHeaders create() {
  ordersService.save(model);
  //addActionMessage("New order created successfully");
  return new DefaultHttpHeaders("success").setLocationId(model.getId());
 }

 // PUT /orders/1
 public String update() {
  ordersService.save(model);
  //addActionMessage("Order updated successfully");
  return "success";
 }

 public void validate() {
  if (model.getClientName() == null
    || model.getClientName().length() == 0) {
   //addFieldError("clientName", "The client name is empty");
  }
 }

 public void setId(String id) {
  if (id != null) {
   this.model = ordersService.get(id);
  }
  this.id = id;
 }

 public Object getModel() {
  return (list != null ? list : model);
 }

}


orders-index.jsp
 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
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
 <title>Orders</title>
</head>
<body>
    <s:actionmessage />
    <table>
        <tr>
            <th>ID</th>
            <th>Client</th>
            <th>Amount</th>
            <th>Actions</th>
        </tr>
        <s:iterator value="model">
        <tr>
            <td>${id}</td>
            <td>${clientName}</td>
            <td>${amount}</td>
            <td><a href="orders/${id}">View</a> |
                <a href="orders/${id}/edit">Edit</a> |
                <a href="orders/${id}/deleteConfirm">Delete</a></td>
        </tr>
        </s:iterator>
    </table>     
    <a href="orders/new">Create a new order</a>
</body>
</html>


web.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
  <!-- <welcome-file>index.jsp</welcome-file> -->
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>