下面列出了怎么用javax.persistence.Lob的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void parse(Context context) {
if (context.getValue() != null) {
MappingRule mappingRule =context.getCurrentMappingRule();
BeanMap beanMap = BeanMap.create(context.getCurrentEntity());
if (context.getValue() != Constants.IGNORE_ERROR_FORMAT_DATA) {
Field field = ReflectionUtils.findField(context.getEntityClass(), mappingRule.getPropertyName());
Column column = field.getAnnotation(Column.class);
if (column.nullable()) {
if (context.getValue() == null) {
throw new DataNullableException(context.getCurrentCell().getRow(), context.getCurrentCell().getCol());
}
}
if (field.getType() == String.class && !field.isAnnotationPresent(Lob.class)) {
String value = (String) context.getValue();
if (value.getBytes().length > column.length()) {
throw new DataLengthException(context.getCurrentCell().getRow(), context.getCurrentCell().getCol(), value, column.length());
}
}
beanMap.put(mappingRule.getPropertyName(), context.getValue());
}
}
}
/**
* 读-文档内容
*
* @return
*/
@Column
@Lob
@MetaProperty(messageKey = "property.fdContent")
default String getFdContent() {
return (String) getExtendProps().get("fdContent");
}
public static void checkLobIndex(List<Class<?>> classes) throws Exception {
for (Class<?> cls : classes) {
List<Field> fields = FieldUtils.getAllFieldsList(cls);
for (Field field : fields) {
Lob lob = field.getAnnotation(Lob.class);
Index index = field.getAnnotation(Index.class);
if ((null != lob) && (null != index)) {
System.err.println(String.format("checkLobIndex error: class: %s, field: %s.", cls.getName(),
field.getName()));
}
}
}
}
private void createStringLobField(Builder builder, Field field) {
// public static final String stringLobValue_FIELDNAME = "stringLobValue";
// @FieldDescribe("长文本.")
// @Lob
// @Basic(fetch = FetchType.EAGER)
// @Column(length = JpaObject.length_10M, name = ColumnNamePrefix + stringLobValue_FIELDNAME)
AnnotationSpec lob = AnnotationSpec.builder(Lob.class).build();
AnnotationSpec basic = AnnotationSpec.builder(Basic.class)
.addMember("fetch", "javax.persistence.FetchType.EAGER").build();
AnnotationSpec column = AnnotationSpec.builder(Column.class).addMember("length", "length_100M")
.addMember("name", "ColumnNamePrefix + " + field.fieldName()).build();
FieldSpec fieldSpec = FieldSpec.builder(String.class, field.getName(), Modifier.PRIVATE)
.addAnnotation(this.fieldDescribe(field)).addAnnotation(lob).addAnnotation(basic).addAnnotation(column)
.build();
MethodSpec get = MethodSpec.methodBuilder("get" + StringUtils.capitalize(field.getName()))
.addModifiers(Modifier.PUBLIC).returns(String.class).addStatement("return this." + field.getName())
.build();
MethodSpec set = MethodSpec.methodBuilder("set" + StringUtils.capitalize(field.getName()))
.addModifiers(Modifier.PUBLIC).returns(void.class).addParameter(String.class, field.getName())
.addStatement("this." + field.getName() + " = " + field.getName()).build();
builder.addField(this.fieldName(field)).addField(fieldSpec).addMethod(get).addMethod(set);
}
@Override
public Optional<Boolean> isFilterable(BeanAttributeInformation attributeDesc) {
Optional<Lob> lob = attributeDesc.getAnnotation(Lob.class);
if (lob.isPresent()) {
return Optional.of(false);
}
return Optional.empty();
}
/**
* @return the {@link Blob} data.
*/
@Lob
@Column(name = "content")
public Blob getData() {
return this.data;
}
@Override
public JdbcType getJdbcType() {
if (isAnnotationPresent(
org.springframework.data.mybatis.annotation.JdbcType.class)) {
return JdbcType.valueOf(getRequiredAnnotation(
org.springframework.data.mybatis.annotation.JdbcType.class).value());
}
if (isAnnotationPresent(Temporal.class)) {
Temporal temporal = getRequiredAnnotation(Temporal.class);
switch (temporal.value()) {
case DATE:
return DATE;
case TIME:
return TIME;
case TIMESTAMP:
return TIMESTAMP;
}
}
Class<?> actualType = getActualType();
if (isAnnotationPresent(Lob.class)) {
if (actualType == String.class) {
return CLOB;
}
return BLOB;
}
JdbcType jdbcType = JAVA_MAPPED_TO_JDBC_TYPES.get(actualType);
return null == jdbcType ? UNDEFINED : jdbcType;
}
/**
* 方法: 取得java.lang.Object
*
* @return: java.lang.Object 备注
*/
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "REMARK", nullable = true)
public java.lang.String getRemark() {
return this.remark;
}
/**
* Gets the ballot summary item.
*
* @return the ballot summary item
*/
@Basic
@Column(name = "BALLOT_SUMMARY_ITEM")
@Lob
public String getBallotSummaryItem() {
return XmlAdapterUtils.unmarshall(ElementAsString.class, this.getBallotSummary());
}
private static boolean isBlob(final Attribute<?, ?> currentAttribute) {
if (currentAttribute != null) {
AnnotatedElement annotatedElement = (AnnotatedElement) currentAttribute.getJavaMember();
if (annotatedElement != null && annotatedElement.getAnnotation(Lob.class) != null) {
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public <T extends Annotation> T getAnnotation(final Class<T> annotationClass) {
if (testCase.equals("temporalnull")) {
return null;
}
if (annotationClass.equals(Temporal.class)) {
if (temporal == null) {
temporal = EasyMock.createMock(Temporal.class);
if (testCase.equals("datetime")) {
EasyMock.expect(temporal.value()).andReturn(TemporalType.TIMESTAMP).anyTimes();
EasyMock.replay(temporal);
} else if (testCase.equals("time")) {
EasyMock.expect(temporal.value()).andReturn(TemporalType.TIME).anyTimes();
EasyMock.replay(temporal);
}
}
return (T) temporal;
} else if (annotationClass.equals(Lob.class)) {
if (testCase.equals("lob")) {
lob = EasyMock.createMock(Lob.class);
EasyMock.replay(lob);
}
return (T) lob;
}
return null;
}
/**
* @return the graphView
*/
@JsonIgnore
@Lob
@Column(length = Integer.MAX_VALUE - 1)
public byte[] getGraphView() {
return graphView;
}
/**
* @return the technologyServiceBytes
*/
@JsonIgnore
@Lob
@Column(length = Integer.MAX_VALUE - 1)
public byte[] getTechnologyServiceBytes() {
return technologyServiceBytes;
}
/**
* @return the dataBytes
*/
@JsonIgnore
@Lob
@Column(length = Integer.MAX_VALUE - 1)
public byte[] getDataBytes() {
return dataBytes;
}
@Lob
@Column(name = "json_obj_str")
public String getJsonObjStr() {
return this.jsonObjStr;
}
@Lob
@Column(name = "log_content")
public String getLogContent() {
return this.logContent;
}
@Lob
public String getV() {
return this.v;
}
/**
*方法: 取得
*@return: 表单模板内容
*/
@Lob
@Column(name ="FTL_CONTENT",nullable=true)
public String getFtlContent(){
return this.ftlContent;
}
@Lob
@Column(name="translations", length=STRING_LENGTH)
public HashMap<String, String> getTranslations() {
return translations;
}
@Override
protected void initAttribute(MetaAttribute attr) {
ManyToMany manyManyAnnotation = attr.getAnnotation(ManyToMany.class);
ManyToOne manyOneAnnotation = attr.getAnnotation(ManyToOne.class);
OneToMany oneManyAnnotation = attr.getAnnotation(OneToMany.class);
OneToOne oneOneAnnotation = attr.getAnnotation(OneToOne.class);
Version versionAnnotation = attr.getAnnotation(Version.class);
Lob lobAnnotation = attr.getAnnotation(Lob.class);
Column columnAnnotation = attr.getAnnotation(Column.class);
boolean idAttr = attr.getAnnotation(Id.class) != null || attr.getAnnotation(EmbeddedId.class) != null;
boolean attrGenerated = attr.getAnnotation(GeneratedValue.class) != null;
attr.setVersion(versionAnnotation != null);
attr.setAssociation(
manyManyAnnotation != null || manyOneAnnotation != null || oneManyAnnotation != null || oneOneAnnotation !=
null);
attr.setLazy(isJpaLazy(attr.getAnnotations(), attr.isAssociation()));
attr.setLob(lobAnnotation != null);
attr.setFilterable(lobAnnotation == null);
attr.setSortable(lobAnnotation == null);
attr.setCascaded(getCascade(manyManyAnnotation, manyOneAnnotation, oneManyAnnotation, oneOneAnnotation));
if (attr.getReadMethod() == null) {
throw new IllegalStateException("no getter found for " + attr.getParent().getName() + "." + attr.getName());
}
Class<?> attributeType = attr.getReadMethod().getReturnType();
boolean isPrimitiveType = ClassUtils.isPrimitiveType(attributeType);
boolean columnNullable = (columnAnnotation == null || columnAnnotation.nullable()) &&
(manyOneAnnotation == null || manyOneAnnotation.optional()) &&
(oneOneAnnotation == null || oneOneAnnotation.optional());
attr.setNullable(!isPrimitiveType && columnNullable);
boolean hasSetter = attr.getWriteMethod() != null;
attr.setInsertable(hasSetter && (columnAnnotation == null || columnAnnotation.insertable()) && !attrGenerated
&& versionAnnotation == null);
attr.setUpdatable(
hasSetter && (columnAnnotation == null || columnAnnotation.updatable()) && !idAttr && versionAnnotation == null);
}
@Column(name = "attachmentcontent",length=3000)
@Lob
public byte[] getAttachmentcontent() {
return this.attachmentcontent;
}
private void getLob(List<Annotation> annotationList, Element element) {
Element subElement = element != null ? element.element( "lob" ) : null;
if ( subElement != null ) {
annotationList.add( AnnotationFactory.create( new AnnotationDescriptor( Lob.class ) ) );
}
}
public Property makeProperty() {
validateMake();
LOG.debugf( "Building property %s", name );
Property prop = new Property();
prop.setName( name );
prop.setValue( value );
prop.setLazy( lazy );
prop.setLazyGroup( lazyGroup );
prop.setCascade( cascade );
prop.setPropertyAccessorName( accessType.getType() );
if ( property != null ) {
prop.setValueGenerationStrategy( determineValueGenerationStrategy( property ) );
if ( property.isAnnotationPresent( AttributeAccessor.class ) ) {
final AttributeAccessor accessor = property.getAnnotation( AttributeAccessor.class );
prop.setPropertyAccessorName( accessor.value() );
}
}
NaturalId naturalId = property != null ? property.getAnnotation( NaturalId.class ) : null;
if ( naturalId != null ) {
if ( ! entityBinder.isRootEntity() ) {
throw new AnnotationException( "@NaturalId only valid on root entity (or its @MappedSuperclasses)" );
}
if ( ! naturalId.mutable() ) {
updatable = false;
}
prop.setNaturalIdentifier( true );
}
// HHH-4635 -- needed for dialect-specific property ordering
Lob lob = property != null ? property.getAnnotation( Lob.class ) : null;
prop.setLob( lob != null );
prop.setInsertable( insertable );
prop.setUpdateable( updatable );
// this is already handled for collections in CollectionBinder...
if ( Collection.class.isInstance( value ) ) {
prop.setOptimisticLocked( ( (Collection) value ).isOptimisticLocked() );
}
else {
final OptimisticLock lockAnn = property != null
? property.getAnnotation( OptimisticLock.class )
: null;
if ( lockAnn != null ) {
//TODO this should go to the core as a mapping validation checking
if ( lockAnn.excluded() && (
property.isAnnotationPresent( javax.persistence.Version.class )
|| property.isAnnotationPresent( Id.class )
|| property.isAnnotationPresent( EmbeddedId.class ) ) ) {
throw new AnnotationException(
"@OptimisticLock.exclude=true incompatible with @Id, @EmbeddedId and @Version: "
+ StringHelper.qualify( holder.getPath(), name )
);
}
}
final boolean isOwnedValue = !isToOneValue( value ) || insertable; // && updatable as well???
final boolean includeInOptimisticLockChecks = ( lockAnn != null )
? ! lockAnn.excluded()
: isOwnedValue;
prop.setOptimisticLocked( includeInOptimisticLockChecks );
}
LOG.tracev( "Cascading {0} with {1}", name, cascade );
this.mappingProperty = prop;
return prop;
}
@Lob
public String getContent() {
return content;
}
@Basic(fetch = LAZY)
@Column(name = "EXTRACT_BINARY")
@Lob
public byte[] getExtractBinary() {
return extractBinary;
}
/**
*方法: 取得byte[]
*@return: byte[] 表单模板内容
*/
@Lob
@Column(name ="FTL_CONTENT",nullable=true)
public String getFtlContent(){
return this.ftlContent;
}
@Column(name = "attachmentcontent",length=3000)
@Lob
public byte[] getAttachmentcontent() {
return this.attachmentcontent;
}
protected void initializeChecks(final Column annotation, final Collection<Check> checks, final AccessibleObject fieldOrMethod) {
/* If the value is generated (annotated with @GeneratedValue) it is allowed to be null
* before the entity has been persisted, same is true in case of optimistic locking
* when a field is annotated with @Version.
* Therefore and because of the fact that there is no generic way to determine if an entity
* has been persisted already, a not-null check will not be performed for such fields.
*/
if (!annotation.nullable() //
&& !fieldOrMethod.isAnnotationPresent(GeneratedValue.class) //
&& !fieldOrMethod.isAnnotationPresent(Version.class) //
&& !fieldOrMethod.isAnnotationPresent(NotNull.class) //
&& !containsCheckOfType(checks, NotNullCheck.class) //
) {
checks.add(new NotNullCheck());
}
// add Length check based on Column.length parameter, but only:
if (!fieldOrMethod.isAnnotationPresent(Lob.class) && // if @Lob is not present
!fieldOrMethod.isAnnotationPresent(Enumerated.class) && // if @Enumerated is not present
!fieldOrMethod.isAnnotationPresent(Length.class) // if an explicit @Length constraint is not present
) {
final LengthCheck lengthCheck = new LengthCheck();
lengthCheck.setMax(annotation.length());
checks.add(lengthCheck);
}
// add Range check based on Column.precision/scale parameters, but only:
if (!fieldOrMethod.isAnnotationPresent(Range.class) // if an explicit @Range is not present
&& annotation.precision() > 0 // if precision is > 0
&& Number.class.isAssignableFrom(fieldOrMethod instanceof Field //
? ((Field) fieldOrMethod).getType() //
: ((Method) fieldOrMethod).getReturnType()) // if numeric field type
) {
/* precision = 6, scale = 2 => -9999.99<=x<=9999.99
* precision = 4, scale = 1 => -999.9<=x<=999.9
*/
final RangeCheck rangeCheck = new RangeCheck();
rangeCheck.setMax(Math.pow(10, annotation.precision() - annotation.scale()) - Math.pow(0.1, annotation.scale()));
rangeCheck.setMin(-1 * rangeCheck.getMax());
checks.add(rangeCheck);
}
}
@Lob
@Column(name = "STUDENT_PHOTO")
public byte[] getPhoto() {
return this.picture;
}
@Lob
@Column(name = "INSTRUCTOR_RESUME", nullable = false)
public char[] getResume() {
return this.resume;
}