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
##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;
/**
* $beanClass is a mapping of $table.getName() Table.
#if ( $table.getRemarks().length() > 0 )
*
Meta Data Information (in progress):
*
* - comments: $table.getRemarks()
*
#end
* @author guyadong
*/
#set( $fieldIndex = 1 )
public class ${beanClass}
implements Serializable,Constant,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.getJavaType() $column.getVarName();
#end
/** columns modified flag */
private $table.stateVarType() modified;
/** columns initialized flag */
private $table.stateVarType() initialized;
/** 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;
}
/**
* @return the modified status of columns
*/
#set( $fieldIndex = $fieldIndex + 1 )
public $table.stateVarType() getModified(){
return modified;
}
/**
* @return the initialized status of columns
*/
#set( $fieldIndex = $fieldIndex + 1 )
public $table.stateVarType() getInitialized(){
return initialized;
}
public ${beanClass}(){
reset();
}
#foreach ( $column in $columns )
#set( $fieldIndex = $fieldIndex + 1 )
/**
* 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()
*/
#if(!$column.isDate() && !$column.isFloat())#swiftThriftField( $fieldIndex $null)#jacksonField($column.getVarName())#end
#if( $column.isDate() || $column.isFloat())#end
public $column.getJavaType() ${column.getGetMethod()}(){
return $column.getVarName();
}
/**
* Setter method for {@link #$column.getVarName()}.
#if ( $column.useEqualsInSetter() )
* The new value is set only if equals() says it is different,
* or if one of either the new value or the current value is null.
* In case the new value is different, it is set and the field is marked as 'modified'.
#elseif ($column.hasCompareTo())
* The new value is set only if compareTo() says it is different,
* or if one of either the new value or the current value is null.
* In case the new value is different, it is set and the field is marked as 'modified'.
#else
* Attention, there will be no comparison with current value which
* means calling this method will mark the field as 'modified' in all cases.
#end
*
* @param newVal the new value#if( !$column.isAutoincrement() && $column.isNotNull())( NOT NULL)#end to be assigned to $column.getVarName()
*/
#if($column.isDate() || $column.isFloat())#end
public void $column.getSetMethod()($column.getJavaType() newVal)
{
$column.bitORAssignExpression("modified");
$column.bitORAssignExpression("initialized");
#if ($column.useEqualsInSetter())
if (Objects.equals(newVal, $column.getVarName())) {
return;
}
#elseif ($column.hasCompareTo())
if (equals(newVal, $column.getVarName())) {
return;
}
#end
$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;
this.modified = $table.maskInitializeWithZero();
this.initialized = $table.maskInitializeWithDefaultValue();
}
@Override
public $beanClass clone(){
try {
return ($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.javaType=='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.javaType=='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.javaType)
*/
public Builder ${column.varName}($column.javaType $column.varName){
TEMPLATE.get().${column.getSetMethod()}($column.varName);
return this;
}
#end
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy