Hibernate configuration
jZeno is integrated with hibernate to perform object-relational mapping. Hibernate needs to be configured to be able to perform this mapping. We will configure hibernate through the hibernate config files:
- hibernate.cfg.xml
- hibernate.properties
A quick overview of the needed configuration will be given, but for more detail, cfr. the hibernate documentation at www.hibernate.org
In hibernate.cfg.xml, we configure the mapping between the database and the objects.
In hibernate.properties, we configure the hibernate framework. This example configures hibernate to work with a local McKoi database. jZeno comes with McKoi built-in to allow you to experiment with it without the need to install a database.
#
#Mon May 15 15:30:04 CEST 2006
hibernate.c3p0.timeout=60
hibernate.connection.driver_class=com.mckoi.JDBCDriver
hibernate.dialect=org.hibernate.dialect.MckoiDialect
hibernate.connection.url=jdbc:mckoi://localhost:9157/
hibernate.connection.username=jzeno
hibernate.connection.password=jzeno
hibernate.show_sql=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.c3p0.max_statements=0
hibernate.c3p0.max_size=100
hibernate.c3p0.idle_test_period=20
hibernate.c3p0.min_size=10
hibernate.hbm2ddl.auto=create
The line hibernate.hbm2ddl.auto=create, tells hibernate will create the tables for you on startup. In fact it will then drop, and re-create your database structure every time the application starts up. In case you are wondering where our test data is coming from, it is inserted by the application before the login page is shown.
Modelbeans
These are classic hibernate POJO’s where with annotations we provide hibernate mapping information. The data from the database will be read into these objects and then jZeno will further process it. Hibernate annotations are used for this.
package net.sf.jzeno.tutorial.model;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "Orders")
@BatchSize(size = 50)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Order extends AbstractTutorialEntity {
private static final long serialVersionUID = 1L;
@OneToMany
private Set orderItems = new HashSet();
@ManyToOne
@JoinColumn(name = "userId")
@BatchSize(size = 50)
private User user;
@Temporal(TemporalType.TIMESTAMP)
private Date orderDate;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Set getOrderItems() {
return orderItems;
}
public void setOrderItems(Set orderItems) {
this.orderItems = orderItems;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date date) {
this.orderDate = date;
}
@Transient
public BigDecimal getPrice() {
BigDecimal ret = new BigDecimal(0).setScale(2,
BigDecimal.ROUND_HALF_EVEN);
for (OrderItem orderItem : orderItems) {
ret = ret.add(orderItem.getProduct().getPrice().multiply(
new BigDecimal(orderItem.getAmount())));
}
return ret;
}
}
Our persistent classes need to implement jZeno's abstract class net.sf.jzeno.model.AbstractEntity, but since we'll be taking advantage of hibernate's optimistic locking strategy which requires a version field we will implement our own abstract superclass.
AbstractTutorialEntity
package net.sf.jzeno.tutorial.model;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import net.sf.jzeno.model.AbstractEntity;
@MappedSuperclass
public class AbstractTutorialEntity extends AbstractEntity {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@SuppressWarnings("unused")
@Version
private Long version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Our User class needs to implement jZeno's User interface so it can take advantage of jZeno's built-in security support.
package net.sf.jzeno.tutorial.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "Users")
@BatchSize(size = 50)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends AbstractTutorialEntity implements
net.sf.jzeno.model.User {
private static final long serialVersionUID = 1L;
@Column(unique = true)
private String userName;
@ManyToMany
@JoinTable(name = "UserRoles")
@BatchSize(size = 50)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set roles = new HashSet();
private String password;
public String getUserName() {
return userName;
}
public boolean hasPermission(String permissionName) {
for (Role role : roles) {
if (role.hasPermission(permissionName)) {
return true;
}
}
return false;
}
public boolean hasRole(String roleName) {
for (Role role : roles) {
if (role.getName().equals(roleName)) {
return true;
}
}
return false;
}
public Set getRoles() {
return roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
Note the methods hasRole(String role) and hasPermission(String permission). These methods are required by the interface net.sf.jzeno.model.User. It is up to your application to implement a suitable strategy for checking permission on a user object... In the example a straight-forward implementation is used. The model objects Role and Permission are iterated to determine the security context of the current user.
| < Prev | Next > |
|---|





