templates.velocity.casban.pertable.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" )
#set( $generateForCasban = true )
#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.List;
##import java.util.Objects;
import gu.sql2java.BaseRow;
import gu.sql2java.exception.RuntimeDaoException;
import com.google.common.base.Throwables;
/**
* $beanClass is a mapping of $table.getName() Table.
#if ( !$table.remarks.empty )
*
Meta Data Information (in progress):
*
* - comments: $table.remarks
*
#end
* @author guyadong
*/
public abstract class ${beanClass} extends BaseRow
#if ($implementsClasses)
implements #foreach( $implements in $implementsClasses )$implements #end
#end
{
################## IMMUTABLE STATUS ###########
protected ${beanClass}(){
reset();
}
#if ( $table.hasPrimaryKey() )
/**
* construct a new instance filled with primary keys
* #join($primaryKeys '@param $e.getVarName() PK# $velocityCount ' '
* ')
*/
protected ${beanClass}(#join($primaryKeys '$e.getJavaType() $e.getVarName()' ',')){
this();
#join($primaryKeys '$e.getSetMethod()($e.getVarName());' '
')
}
#end
#foreach ( $column in $columns )
/**
* Getter method for column '$column.varName'
#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.remarks.empty )
* - comments: $column.remarks
#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.varName'
*/
public abstract $column.getJavaType() ${column.getGetMethod()}();
#if(($swiftBeanSupport || $jacksonBeanSupport ) && $column.isDate())
/**
* use Long to represent date type for thrift:swift support
* @see #${column.getGetMethod()}()
*/
public abstract $dateSerializeType ${column.getReadMethod()}();
#end
#if(($swiftBeanSupport || $jacksonBeanSupport ) && $column.isFloat())
/**
* use Double to represent Float type for thrift:swift support
* @see #${column.getGetMethod()}()
*/
public abstract Double ${column.getReadMethod()}();
#end
/**
* Setter method for column '$column.varName'
#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.varName'
*/
public abstract void $column.getSetMethod()($column.getJavaType() newVal);
################ READ/WRITE METHOD FOR JSON & GEOMETRY FIELD & Custom Field Type###############
#if($column.jsonJacksonRawValue || ($column.geometry && $codewriter.geometrySerialType == "STRING") || $column.definedFieldType)
/**
* read method for column '$column.varName'
* @return the {@link $column.rwType} value of '$column.varName'
*/
public abstract $column.rwType ${column.ReadMethod}();
/**
* write method for column '$column.varName' with {@link $column.rwType} object.
*/
public abstract void ${column.writeMethod}($column.rwType newVal);
#end
################ SETTER METHOD FOR SWIFT OR JACKSON ###############
#if(($swiftBeanSupport || $jacksonBeanSupport ) && ($column.isDate() || $column.isFloat()))
/**
* setter for thrift:swift OR jackson support
* without modification for field 'modified' and 'initialized'
* NOTE:DO NOT use the method in your code
*/
#if($column.isDate())
public abstract void ${column.getWriteMethod()}($dateSerializeType newVal);
#elseif($column.isFloat())
public abstract void ${column.getWriteMethod()}(Double newVal);
#end
#end###if(($swiftBeanSupport || $jacksonBeanSupport ) && ($column.isDate() || $column.isFloat()))
#if ( $column.hasPrimaryType() )
#if($primitiveSetterSupport && !$column.definedFieldType)
/**
* Setter method for column '$column.varName'
* Convenient for those who do not want to deal with Objects for primary types.
*
* @param newVal the new value to be assigned to '$column.varName'
*/
public abstract void $column.getSetMethod()($column.getJavaPrimaryType() newVal);
#end
#if($column.isDate() && $dateLongSetterSupport)
/**
* Setter method for column '$column.varName'
* @param newVal the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
*/
public abstract void $column.getSetMethod()(Long newVal);
#end
#end##if( $column.hasPrimaryType() )
/**
* Determines if the '$column.varName' has been modified.
*
* @return true if the field has been modified, false if the field has not been modified
*/
public abstract boolean ${column.getModifiedMethod()}();
/**
* Determines if the '$column.varName' has been initialized.
*
* It is useful to determine if a field is null on purpose or just because it has not been initialized.
*
* @return true if the field has been initialized, false otherwise
*/
public abstract boolean ${column.getInitializedMethod()}();
#end
/** reset all fields to initial value, equal to a new bean */
public abstract void reset();
//1.2
/**
* Loads a $beanClass bean from the table using primary key fields of {@code bean}.
* if exists,copy to this bean.
* @return a unique B or {@code null} if not found or bean is null
* @throws RuntimeDaoException
*/
public abstract $beanClass load()throws RuntimeDaoException;
//1.6
/**
* Returns true if this table contains row specified by primary key fields of $beanClass.
* when you don't know which is primary key of table,you can use the method.
* @throws RuntimeDaoException
*/
public abstract boolean exists()throws RuntimeDaoException;
//2.2
/**
* Delete row according to primary keys of bean.
*
* @return the number of deleted rows,0 returned if bean is null
* @throws RuntimeDaoException
*/
public abstract int delete()throws RuntimeDaoException;
//_____________________________________________________________________
//
// SAVE
//_____________________________________________________________________
//12
/**
* Saves the $beanClass bean into the database.
*
* @return the inserted or updated bean,or null if bean is null
* @throws RuntimeDaoException
*/
public abstract $beanClass save()throws RuntimeDaoException;
//12-1
/**
* If the specified key is not already exist, add it to database.
* This is equivalent to
* {@code
* if (!existsByPrimaryKey(bean))
* return insert(bean);
* else
* return loadByPrimaryKey(bean);
* }
*
* except that the action is performed atomically .
* @return the previous value exists in database, or saved bean if not exists bean ,
* or {@code null} if bean is {@code null}
* @throws RuntimeDaoException
*/
public abstract $beanClass addIfAbsent() throws RuntimeDaoException;
//19-2
/**
* Loads a list of $beanClass bean from a template one.
*
* @return all the $beanClass beans matching the template
* @throws RuntimeDaoException
*/
public abstract List loadUsingTemplate()throws RuntimeDaoException;
#foreach ($importedTable in $importedTables)
#if ( $velocityCount == 1 )
//////////////////////////////////////
// GET/SET IMPORTED KEY BEAN METHOD
//////////////////////////////////////
#end
#set ( $importedClass = "$importedTable.asBeanClassNSP()" )
#set ( $importedNative = "$importedTable.asBeanClass()" )
#set ( $importedClassManager = "$importedTable.asManagerInterfaceNSP()" )
#foreach( $fkName in $importedTable.getFkMapNames($table.name) )
#set ( $columnsOfFk = $importedTable.getForeignKeysByFkName($fkName) )
//3.2 GET IMPORTED
/**
* Retrieves the {@link $importedClass} object from the #join($columnsOfFk "$e.fullName" ",") field.
* FK_NAME : $fkName
* @return import bean list
* @throws RuntimeDaoException
*/
public abstract List ${importedTable.readImportedBeansGetMethod($fkName)}()throws RuntimeDaoException;
//3.2.3 DELETE IMPORTED
/**
* delete the associated {@link $importedClass} objects from #join($columnsOfFk "$e.fullName" ",") field.
* FK_NAME:$fkName
* @return the number of deleted rows
* @throws RuntimeDaoException
*/
public abstract int ${importedTable.getImportedBeansDelMethod($fkName)}()throws RuntimeDaoException;
#end
#end###foreach ($importedTable in $importedTables)
## ======================================================
## xForeignKeys template
## ======================================================
#foreach ($foreignTable in $foreignTables)
#if ( $velocityCount == 1 )
//////////////////////////////////////
// GET/SET FOREIGN KEY BEAN METHOD
//////////////////////////////////////
#end
#set ( $foreignClass = "$foreignTable.asBeanClassNSP()" )
#set ( $foreignNative = "$foreignTable.asBeanClass()" )
#foreach( $fkName in $table.getFkMapNames($foreignTable.name) )
#set ( $columnsOfFk = $table.getForeignKeysByFkName($fkName) )
//5.1 GET REFERENCED VALUE
/**
* Retrieves the {@link $foreignClass} object referenced by #join($columnsOfFk "{@link $beanClass#$e.getGetMethod()}()" ",") field.
* FK_NAME : $fkName
* @return the associated {@link $foreignClass} bean or {@code null} if {@code bean} is {@code null}
*/
public abstract F $table.readReferencedVarGetMethod($fkName)();
#end
#end
@Override
public $beanClass clone(){
return ($beanClass) super.clone();
}
############################################
public static final Builder cellBuilder(){
return new Builder().reset();
}
#if ( $table.hasPrimaryKey() )
public static final Builder cellBuilder(#join($primaryKeys '$e.javaType $e.varName' ',')){
return new Builder().reset()#join($primaryKeys '.${e.varName}($e.varName)' '
');
}
#end
/**
* 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() {
try{
return ($beanClass)Constant.${table.asConstantVar()}_CLASS.newInstance();
}catch(Exception e){
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}};
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}*/
@SuppressWarnings("unchecked")
public B build(){
return (B)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
}
}