下面列出了怎么用javax.persistence.Temporal的API类实例代码及写法,或者点击链接到github查看源代码。
private void createDateField(Builder builder, Field field) {
AnnotationSpec column = AnnotationSpec.builder(Column.class)
.addMember("name", "ColumnNamePrefix + " + field.fieldName()).build();
AnnotationSpec temporal = AnnotationSpec.builder(Temporal.class)
.addMember("value", "javax.persistence.TemporalType.DATE").build();
FieldSpec fieldSpec = FieldSpec.builder(Date.class, field.getName(), Modifier.PRIVATE)
.addAnnotation(this.fieldDescribe(field)).addAnnotation(this.index(field))
.addAnnotation(this.checkPersist(field)).addAnnotation(column).addAnnotation(temporal).build();
MethodSpec get = MethodSpec.methodBuilder("get" + StringUtils.capitalize(field.getName()))
.addModifiers(Modifier.PUBLIC).returns(Date.class).addStatement("return this." + field.getName())
.build();
MethodSpec set = MethodSpec.methodBuilder("set" + StringUtils.capitalize(field.getName()))
.addModifiers(Modifier.PUBLIC).returns(void.class).addParameter(Date.class, field.getName())
.addStatement("this." + field.getName() + " = " + field.getName()).build();
builder.addField(this.fieldName(field)).addField(fieldSpec).addMethod(get).addMethod(set);
}
private void createDateTimeField(Builder builder, Field field) {
AnnotationSpec column = AnnotationSpec.builder(Column.class)
.addMember("name", "ColumnNamePrefix + " + field.fieldName()).build();
AnnotationSpec temporal = AnnotationSpec.builder(Temporal.class)
.addMember("value", "javax.persistence.TemporalType.TIMESTAMP").build();
FieldSpec fieldSpec = FieldSpec.builder(Date.class, field.getName(), Modifier.PRIVATE)
.addAnnotation(this.fieldDescribe(field)).addAnnotation(this.index(field))
.addAnnotation(this.checkPersist(field)).addAnnotation(column).addAnnotation(temporal).build();
MethodSpec get = MethodSpec.methodBuilder("get" + StringUtils.capitalize(field.getName()))
.addModifiers(Modifier.PUBLIC).returns(Date.class).addStatement("return this." + field.getName())
.build();
MethodSpec set = MethodSpec.methodBuilder("set" + StringUtils.capitalize(field.getName()))
.addModifiers(Modifier.PUBLIC).returns(void.class).addParameter(Date.class, field.getName())
.addStatement("this." + field.getName() + " = " + field.getName()).build();
builder.addField(this.fieldName(field)).addField(fieldSpec).addMethod(get).addMethod(set);
}
private void getTemporal(List<Annotation> annotationList, Element element) {
Element subElement = element != null ? element.element( "temporal" ) : null;
if ( subElement != null ) {
AnnotationDescriptor ad = new AnnotationDescriptor( Temporal.class );
String temporal = subElement.getTextTrim();
if ( "DATE".equalsIgnoreCase( temporal ) ) {
ad.setValue( "value", TemporalType.DATE );
}
else if ( "TIME".equalsIgnoreCase( temporal ) ) {
ad.setValue( "value", TemporalType.TIME );
}
else if ( "TIMESTAMP".equalsIgnoreCase( temporal ) ) {
ad.setValue( "value", TemporalType.TIMESTAMP );
}
else if ( StringHelper.isNotEmpty( temporal ) ) {
throw new AnnotationException( "Unknown TemporalType: " + temporal + ". " + SCHEMA_VALIDATION );
}
annotationList.add( AnnotationFactory.create( ad ) );
}
}
@Override
public void addGetMethod(String name, Method method) {
getMethods.put(name, method);
Jsonable jsonable = method.getAnnotation(Jsonable.class);
if (jsonable != null)
jsonables.put(name, jsonable);
ManyToOne manyToOne = method.getAnnotation(ManyToOne.class);
if (manyToOne != null)
manyToOnes.put(name, manyToOne);
Class<?> type = method.getReturnType();
Temporal temporal = method.getAnnotation(Temporal.class);
if (temporal != null) {
if (TemporalType.DATE.equals(temporal.value()))
type = Date.class;
else if (TemporalType.TIMESTAMP.equals(temporal.value()))
type = Timestamp.class;
}
types.put(name, type);
addLowerCase(name);
}
/**
* Gets the registered date.
*
* @return the registered date
*/
@Basic
@Column(name = "REGISTERED_DATE")
@Temporal(TemporalType.DATE)
public Date getRegisteredDate() {
return registeredDate;
}
/**
* Gets the made public date.
*
* @return the made public date
*/
@Basic
@Column(name = "MADE_PUBLIC_DATE")
@Temporal(TemporalType.DATE)
public Date getMadePublicDate() {
return madePublicDate;
}
/**
* Gets the created date.
*
* @return the created date
*/
@Basic
@Column(name = "CREATED_DATE")
@Temporal(TemporalType.DATE)
public Date getCreatedDate() {
return createdDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "INS_CLASSES_END_DATE", length = 7)
public Date getEndDate() {
return this.endDate;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "acc_time")
public Date getAccTime() {
return this.accTime;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "start_time")
public Date getStartTime() {
return this.startTime;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "version_date")
public Date getVersionDate() {
return this.versionDate;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "LOG_DATE", length = 19)
public Date getLogDate() {
return this.logDate;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "UPDATE_DATE", length = 19)
public Date getUpdateDate() {
return this.updateDate;
}
/**
* Gets the creates the date.
*
* @return the creates the date
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "createDate", length = 19)
public Date getCreateDate() {
return this.createDate;
}
/**
* Gets the modified date.
*
* @return the modified date
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modifiedDate", length = 19)
public Date getModifiedDate() {
return this.modifiedDate;
}
/**
* Gets the last checked in.
*
* @return the last checked in
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "lastCheckedIn", length = 19)
public Date getLastCheckedIn() {
return this.lastCheckedIn;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getLastLogin() {return lastLogin;}
@Temporal(TemporalType.TIMESTAMP)
public Date getPresignedUrlExpires() {return this.presignedUrlExpires;}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "FAVORITE_DATE", length = 19)
public Date getFavoriteDate() {
return this.favoriteDate;
}
private void applyAttributeConverter(XProperty property, ConverterDescriptor attributeConverterDescriptor) {
if ( attributeConverterDescriptor == null ) {
return;
}
LOG.debugf( "Starting applyAttributeConverter [%s:%s]", persistentClassName, property.getName() );
if ( property.isAnnotationPresent( Id.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for Id attribute [%s]", property.getName() );
return;
}
if ( isVersion ) {
LOG.debugf( "Skipping AttributeConverter checks for version attribute [%s]", property.getName() );
return;
}
if ( !key && property.isAnnotationPresent( Temporal.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for Temporal attribute [%s]", property.getName() );
return;
}
if ( key && property.isAnnotationPresent( MapKeyTemporal.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for map-key annotated as MapKeyTemporal [%s]", property.getName() );
return;
}
if ( !key && property.isAnnotationPresent( Enumerated.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for Enumerated attribute [%s]", property.getName() );
return;
}
if ( key && property.isAnnotationPresent( MapKeyEnumerated.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for map-key annotated as MapKeyEnumerated [%s]", property.getName() );
return;
}
if ( isAssociation() ) {
LOG.debugf( "Skipping AttributeConverter checks for association attribute [%s]", property.getName() );
return;
}
this.attributeConverterDescriptor = attributeConverterDescriptor;
}
@Temporal(TemporalType.DATE)
@Column(name = "birthDate", nullable = false, length = 10)
public Date getBirthDate() {
return this.birthDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "startDate", nullable = false, length = 10)
public Date getStartDate() {
return this.startDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "expiry", nullable = false, length = 10)
public Date getExpiry() {
return this.expiry;
}
@Temporal(TemporalType.DATE)
@Column(name = "birthDate", nullable = false, length = 10)
public Date getBirthDate() {
return this.birthDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "startDate", nullable = false, length = 10)
public Date getStartDate() {
return this.startDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "birthDate", length = 10)
public Date getBirthDate() {
return this.birthDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "date", nullable = false, length = 10)
public Date getDate() {
return this.date;
}
@Temporal(TemporalType.DATE)
@Column(name = "birthDate", nullable = false, length = 10)
public Date getBirthDate() {
return this.birthDate;
}
@Temporal(TemporalType.DATE)
@Column(name = "date", nullable = false, length = 10)
public Date getDate() {
return this.date;
}
@Temporal(TemporalType.DATE)
@Column(name = "birthDate", nullable = false, length = 10)
public Date getBirthDate() {
return this.birthDate;
}