templates.velocity.simplebean.pertable.simple.bean.vm Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-generator Show documentation
Show all versions of sql2java-generator Show documentation
executable jar of sql2java generator
The newest version!
##included template: bean.include.vm
#parse( "header.include.vm" )
#parse( "table.include.vm" )
#parse( "macros.include.vm" )
#set ( $beanClass = $table.asBeanClass($isGeneral) )
#set ( $beanClassFullName = "${table.package}.${beanClass}" )
$codewriter.setCurrentJavaFilename(${table.package}, "${beanClass}.java")
package ${table.package};
import java.io.Serializable;
import java.util.Objects;
import static gu.sql2java.utils.DeepCloneUtils.cloneFields;
/**
* $beanClass is a mapping of $table.getName() Table.
#if ( $table.getRemarks().length() > 0 )
*
Meta Data Information (in progress):
*
* - comments: $table.getRemarks()
*
#end
* @author guyadong
*/
public class ${beanClass}
implements Serializable,Cloneable
{
private static final long serialVersionUID = ${table.getSerialVersionUID($beanClassFullName)}L;
#foreach ( $column in $columns )
#if ( $column.getRemarks().length() > 0 )
/** comments:$column.getRemarks() */
#end
private $column.fieldJavaType $column.getVarName();
#end
/** new record flag */
private boolean isNew;
################## IMMUTABLE STATUS ###########
public boolean isNew()
{
return this.isNew;
}
/**
* Specifies to the object if it has been set as new.
*
* @param isNew the boolean value to be assigned to the isNew field
*/
public void setNew(boolean isNew)
{
this.isNew = isNew;
}
public ${beanClass}(){
reset();
}
#foreach ( $column in $columns )
/**
* Getter method for {@link #$column.getVarName()}.
#if ( $column.isPrimaryKey() )
* PRIMARY KEY.
#end
* Meta Data Information (in progress):
*
* - full name: $column.getFullName()
#foreach ( $fKey in $column.getForeignKeys() )
* - foreign key: ${fKey.getTableName()}.${fKey.getName()}
#end
#foreach ( $iKey in $column.getImportedKeys() )
* - imported key: ${iKey.getTableName()}.${iKey.getName()}
#end
#if ( !$column.getRemarks().empty )
* - comments: $column.getRemarks()
#end
#if ( $column.getOriginalDefaultValue() )
* - default value: '$column.getOriginalDefaultValue()'
#end
#if ($column.isAutoincrement())
* - AUTO_INCREMENT
#end
#if ($column.isNotNull())
* - NOT NULL
#end
* - column size: $column.size
* - JDBC type returned by the driver: $column.getJavaTypeAsTypeName()
*
*
* @return the value of $column.getVarName()
*/
public $column.fieldJavaType ${column.getGetMethod()}(){
return $column.getVarName();
}
/**
* Setter method for {@link #$column.getVarName()}.
*
* @param newVal the new value
*/
public void $column.getSetMethod()($column.fieldJavaType newVal)
{
$column.getVarName() = newVal;
}
#end
/** reset all fields to initial value, equal to a new bean */
public void reset(){
#foreach($column in $columns)
#if($column.originalDefaultValue)
### Default value
$!{column.commentOfDefaultValue()}
#end
this.$column.getVarName()${column.getDefaultValueAssignment(true)};
#end
this.isNew = true;
}
@Override
public $beanClass clone(){
try {
return cloneFields(($beanClass) super.clone());
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
#foreach($column in $columns)
#if($column.fieldJavaType =='byte[]')
result = prime * result + ((${column.varName} == null) ? 0 : java.nio.ByteBuffer.wrap(${column.varName}).hashCode());
#else
result = prime * result + ((${column.varName} == null) ? 0 : ${column.varName}.hashCode());
#end
#end###foreach($column in $columns)
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof $beanClass))
return false;
$beanClass other = ($beanClass) obj;
#foreach($column in $columns)
#if($column.fieldJavaType=='byte[]')
if(null == ${column.varName} ){
if(null != other.${column.varName})
return false;
}else if(null == other.${column.varName})
return false
else if(!java.nio.ByteBuffer.wrap(${column.varName}).equals(java.nio.ByteBuffer.wrap(other.${column.varName})))
return false;
#else
if(!Objects.equals(${column.varName},other.${column.varName}))
return false;
#end
#end###foreach($column in $columns)
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("$beanClass [");
#foreach($column in $columns)
builder.append("#if($foreach.index > 0),#end${column.varName}=").append(${column.varName});
#end###foreach($column in $columns)
builder.append("]");
return builder.toString();
}
############################################
public static final Builder builder(){
return new Builder().reset();
}
/**
* a builder for $beanClass,the template instance is thread local variable
* a instance of Builder can be reused.
*/
public static final class Builder{
/** $beanClass instance used for template to create new $beanClass instance. */
static final ThreadLocal<$beanClass> TEMPLATE = new ThreadLocal<$beanClass>(){
@Override
protected $beanClass initialValue() {
return new ${beanClass}();
}};
private Builder() {}
/**
* reset the bean as template
* @see ${beanClass}${esc.hash}reset()
*/
public Builder reset(){
TEMPLATE.get().reset();
return this;
}
/** set a bean as template,must not be {@code null} */
public Builder template($beanClass bean){
if(null == bean){
throw new NullPointerException();
}
TEMPLATE.set(bean);
return this;
}
/** return a clone instance of {@link ${esc.hash}TEMPLATE}*/
public $beanClass build(){
return TEMPLATE.get().clone();
}
#foreach($column in $columns)
/**
* fill the field : $column.fullName
* @param $column.varName $!{column.remarks}
* @see $beanClass${esc.hash}${column.getGetMethod()}()
* @see $beanClass${esc.hash}${column.getSetMethod()}($column.fieldJavaType)
*/
public Builder ${column.varName}($column.fieldJavaType $column.varName){
TEMPLATE.get().${column.getSetMethod()}($column.varName);
return this;
}
#end
}
}