Mapping
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping default-cascade="none">
<class name="nl.dennie.vikie.model.Range" table="ranges">
<meta attribute="class-code">
<![CDATA[
public synchronized long reserve(int block){
if(getAmountAvailable().intValue()<block){
return -1;
}
else{
setAmountAvailable(new Integer(getAmountAvailable().intValue() - block));
return getTill().longValue()-getAmountAvailable().intValue();
}
}
]]>
</meta>
<id name="id" type="int" column="ID">
<generator class="identity"/>
</id>
<property name="name" column="NAME" type="string"/>
<property name="from" column="FROMVAL" type="long"/>
<property name="amountAvailable" column="AVAILABLE" type="int"/>
<property name="till" column="TILLVAL" type="long"/>
<property name="timeOfCreation" column="TIME_OF_CREATION" type="calendar"/>
<many-to-one name="customer" class="nl.dennie.vikie.model.Customer" column="CUSTOMERID" />
<many-to-one name="parent" class="nl.dennie.vikie.model.Range" column="PARENTID" />
</class>
</hibernate-mapping>
GeneratesCode:
package nl.dennie.vikie.model;
import java.io.Serializable;
import java.util.Calendar;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Range implements Serializable {
/** identifier field */
private Integer id;
/** nullable persistent field */
private String name;
/** nullable persistent field */
private Long from;
/** nullable persistent field */
private Integer amountAvailable;
/** nullable persistent field */
private Long till;
/** nullable persistent field */
private Calendar timeOfCreation;
/** nullable persistent field */
private nl.dennie.vikie.model.Customer customer;
/** nullable persistent field */
private nl.dennie.vikie.model.Range parent;
/** full constructor */
public Range(String name, Long from, Integer amountAvailable, Long till, Calendar timeOfCreation, nl.dennie.vikie.model.Customer customer, nl.dennie.vikie.model.Range parent) {
this.name = name;
this.from = from;
this.amountAvailable = amountAvailable;
this.till = till;
this.timeOfCreation = timeOfCreation;
this.customer = customer;
this.parent = parent;
}
/** default constructor */
public Range() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Long getFrom() {
return this.from;
}
public void setFrom(Long from) {
this.from = from;
}
public Integer getAmountAvailable() {
return this.amountAvailable;
}
public void setAmountAvailable(Integer amountAvailable) {
this.amountAvailable = amountAvailable;
}
public Long getTill() {
return this.till;
}
public void setTill(Long till) {
this.till = till;
}
public Calendar getTimeOfCreation() {
return this.timeOfCreation;
}
public void setTimeOfCreation(Calendar timeOfCreation) {
this.timeOfCreation = timeOfCreation;
}
public nl.dennie.vikie.model.Customer getCustomer() {
return this.customer;
}
public void setCustomer(nl.dennie.vikie.model.Customer customer) {
this.customer = customer;
}
public nl.dennie.vikie.model.Range getParent() {
return this.parent;
}
public void setParent(nl.dennie.vikie.model.Range parent) {
this.parent = parent;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
// The following is extra code specified in the hbm.xml files
public synchronized long reserve(int block){
if(getAmountAvailable().intValue()<block){
return -1;
}
else{
setAmountAvailable(new Integer(getAmountAvailable().intValue() - block));
return getTill().longValue()-getAmountAvailable().intValue();
}
}
// end of extra code specified in the hbm.xml files
}
I am using the Hibernate tools plugin with IDEA.