下面列出了怎么用javax.persistence.Version的API类实例代码及写法,或者点击链接到github查看源代码。
public static Property<Serializable> getVersionProperty(Class<?> entityClass)
{
List<PropertyCriteria> criteriaList = new LinkedList<PropertyCriteria>();
criteriaList.add(new AnnotatedPropertyCriteria(Version.class));
String fromMappingFiles = PersistenceUnitDescriptorProvider.getInstance().versionField(entityClass);
if (fromMappingFiles != null)
{
criteriaList.add(new NamedPropertyCriteria(fromMappingFiles));
}
for (PropertyCriteria criteria : criteriaList)
{
PropertyQuery<Serializable> query =
PropertyQueries.<Serializable> createQuery(entityClass).addCriteria(criteria);
Property<Serializable> result = query.getFirstResult();
if (result != null)
{
return result;
}
}
return null;
}
/**
* 构造方法
*
* @param metadata
*/
HibernateMetadata(Class<?> clazz) {
ormClass = clazz;
ormName = clazz.getName();
ReflectionUtility.doWithFields(ormClass, (field) -> {
if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) {
return;
}
if (field.isAnnotationPresent(Version.class)) {
versionName = field.getName();
return;
}
Class<?> type = ClassUtility.primitiveToWrapper(field.getType());
if (String.class == type) {
fields.put(field.getName(), type);
} else if (type.isEnum()) {
fields.put(field.getName(), type);
} else if (Collection.class.isAssignableFrom(type) || type.isArray()) {
fields.put(field.getName(), List.class);
} else if (Date.class.isAssignableFrom(type)) {
fields.put(field.getName(), Date.class);
} else {
fields.put(field.getName(), Map.class);
}
if (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)) {
primaryName = field.getName();
primaryClass = type;
}
});
Table table = clazz.getAnnotation(Table.class);
if (table != null) {
for (Index index : table.indexes()) {
indexNames.add(index.columnList());
}
}
}
@Override
public Optional<Boolean> isPostable(BeanAttributeInformation attributeDesc) {
Optional<Column> column = attributeDesc.getAnnotation(Column.class);
Optional<Version> version = attributeDesc.getAnnotation(Version.class);
if (!version.isPresent() && column.isPresent()) {
return Optional.of(column.get().insertable());
}
Optional<GeneratedValue> generatedValue = attributeDesc.getAnnotation(GeneratedValue.class);
if (generatedValue.isPresent()) {
return Optional.of(false);
}
return Optional.empty();
}
@Override
public Optional<Boolean> isPatchable(BeanAttributeInformation attributeDesc) {
Optional<Column> column = attributeDesc.getAnnotation(Column.class);
Optional<Version> version = attributeDesc.getAnnotation(Version.class);
if (!version.isPresent() && column.isPresent()) {
return Optional.of(column.get().updatable());
}
Optional<GeneratedValue> generatedValue = attributeDesc.getAnnotation(GeneratedValue.class);
if (generatedValue.isPresent()) {
return Optional.of(false);
}
return Optional.empty();
}
private boolean hasJpaAnnotations(MetaAttribute attribute) {
List<Class<? extends Annotation>> annotationClasses = Arrays.asList(Id.class, EmbeddedId.class, Column.class,
ManyToMany.class, ManyToOne.class, OneToMany.class, OneToOne.class, Version.class,
ElementCollection.class);
for (Class<? extends Annotation> annotationClass : annotationClasses) {
if (attribute.getAnnotation(annotationClass) != null) {
return true;
}
}
return false;
}
@StoreType("contentstore")
@RequestMapping(value = BASE_MAPPING, method = RequestMethod.DELETE)
@ResponseBody
public ResponseEntity<?> deleteContent(@RequestHeader HttpHeaders headers,
@PathVariable String repository,
@PathVariable String id,
@PathVariable String contentProperty,
@PathVariable String contentId)
throws HttpRequestMethodNotSupportedException {
Object domainObj = findOne(repositories, repository, id);
String etag = (BeanUtils.getFieldWithAnnotation(domainObj, Version.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, Version.class).toString() : null);
Object lastModifiedDate = (BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) : null);
HeaderUtils.evaluateHeaderConditions(headers, etag, lastModifiedDate);
PersistentProperty<?> property = this.getContentPropertyDefinition(
repositories.getPersistentEntity(domainObj.getClass()), contentProperty);
Object contentPropertyValue = getContentProperty(domainObj, property, contentId);
Class<?> contentEntityClass = ContentPropertyUtils.getContentPropertyType(property);
ContentStoreInfo info = ContentStoreUtils.findContentStore(storeService, contentEntityClass);
info.getImpementation().unsetContent(contentPropertyValue);
// remove the content property reference from the data object
// setContentProperty(domainObj, property, contentId, null);
save(repositories, domainObj);
return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
private void replaceContentInternal(HttpHeaders headers, Repositories repositories,
ContentStoreService stores, String repository, String id,
String contentProperty, String contentId, String mimeType,
String originalFileName, InputStream stream)
throws HttpRequestMethodNotSupportedException {
Object domainObj = findOne(repositories, repository, id);
String etag = (BeanUtils.getFieldWithAnnotation(domainObj, Version.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, Version.class).toString() : null);
Object lastModifiedDate = (BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) : null);
HeaderUtils.evaluateHeaderConditions(headers, etag, lastModifiedDate);
PersistentProperty<?> property = this.getContentPropertyDefinition(repositories.getPersistentEntity(domainObj.getClass()), contentProperty);
Object contentPropertyValue = this.getContentProperty(domainObj, property, contentId);
if (BeanUtils.hasFieldWithAnnotation(contentPropertyValue, MimeType.class)) {
BeanUtils.setFieldWithAnnotation(contentPropertyValue, MimeType.class,
mimeType);
}
if (originalFileName != null && StringUtils.hasText(originalFileName)) {
if (BeanUtils.hasFieldWithAnnotation(contentPropertyValue,
OriginalFileName.class)) {
BeanUtils.setFieldWithAnnotation(contentPropertyValue,
OriginalFileName.class, originalFileName);
}
}
Class<?> contentEntityClass = ContentPropertyUtils.getContentPropertyType(property);
ContentStoreInfo info = ContentStoreUtils.findContentStore(storeService, contentEntityClass);
info.getImpementation().setContent(contentPropertyValue, stream);
save(repositories, domainObj);
}
protected Object lock(Object entity) {
if (em == null) {
return entity;
}
if (BeanUtils.hasFieldWithAnnotation(entity, Version.class) == false) {
return entity;
}
entity = em.merge(entity);
em.lock(entity, LockModeType.OPTIMISTIC);
return entity;
}
public static EntityMeta extract(Class<?> clazz) {
if(registeredTable.containsKey(clazz)) {
return registeredTable.get(clazz);
}
String tableName = null;
TableDefinition td = null;
Map<String, ColumnDefinition> colMap = new HashMap<>();
try {
Field tableDef = clazz.getDeclaredField(clazz.getSimpleName().toUpperCase());
td = (TableDefinition)tableDef.get(null);
tableName = td.getName();
} catch (Throwable e) {
// It is OK for only the query entity
}
if(td != null) {
for(ColumnDefinition colDef: td.allColumns()) {
colMap.put(colDef.getColumnName(), colDef);
}
}
Field[] allFields = clazz.getDeclaredFields();
if (null == allFields || allFields.length == 0) {
throw new IllegalArgumentException("The entity[" + clazz.getName() +"] has no fields.");
}
Map<String, Field> fieldMap = new HashMap<>();
Field idField = null;
List<ColumnMeta> columns = new ArrayList<>();
for (Field f: allFields) {
Column column = f.getAnnotation(Column.class);
Id id = f.getAnnotation(Id.class);
// Column must be specified
if (column == null) {
continue;
}
String columnName = column.name().trim().length() == 0 ? f.getName() : column.name();
if(tableName != null) {
Objects.requireNonNull(colMap.get(columnName), "Column " + columnName + " must be defined in " + tableName);
}
f.setAccessible(true);
fieldMap.put(columnName, f);
GeneratedValue generatedValue = f.getAnnotation(GeneratedValue.class);
boolean autoIncremental = null != generatedValue &&
(generatedValue.strategy() == GenerationType.AUTO || generatedValue.strategy() == GenerationType.IDENTITY);
if(idField != null && autoIncremental) {
throw new IllegalArgumentException("Found duplicate Id columns");
} else if (id != null) {
idField = f;
}
JDBCType type = colMap.containsKey(columnName) ? colMap.get(columnName).getType() : null;
columns.add(new ColumnMeta(
columnName,
type,
autoIncremental,
id != null,
column.insertable(),
column.updatable(),
f.getAnnotation(Version.class) != null));
}
if(tableName!= null && columns.size() != colMap.size()) {
throw new IllegalArgumentException("The columns defined in table definition does not match the columns defined in class");
}
EntityMeta tableMeta = new EntityMeta(td, columns.toArray(new ColumnMeta[0]), fieldMap, idField);
registeredTable.putIfAbsent(clazz, tableMeta);
return registeredTable.get(clazz);
}
@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);
}
@Version
public Integer getVersion() {
return this.version;
}
@Version
public Integer getVersion() {
return this.version;
}
@Override
@Version
public int getModificationCounter() {
return this.modificationCounter;
}
@Version
@Column(name = "version", nullable = false)
public Long getVersion() {
return version;
}
@Version
@Column(name = "version", nullable = false)
public long getVersion() {
return this.version;
}
@Version
@Column(name = "version", nullable = false)
public long getVersion() {
return this.version;
}
@Column(name = "VERSION", precision = 10)
@Version
public Integer getVersion() {
return version;
}
@Override
protected MetaAttribute createAttribute(T metaDataObject, PropertyDescriptor desc) {
MetaEntityAttribute attr = new MetaEntityAttribute();
attr.setName(desc.getName());
attr.setParent(metaDataObject, true);
if (hasJpaAnnotations(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);
ElementCollection elemCollectionAnnotation = attr.getAnnotation(ElementCollection.class);
attr.setVersion(versionAnnotation != null);
FetchType fetchType = null;
if (manyManyAnnotation != null) {
fetchType = manyManyAnnotation.fetch();
}
if (oneManyAnnotation != null) {
fetchType = oneManyAnnotation.fetch();
}
if (oneOneAnnotation != null) {
fetchType = oneOneAnnotation.fetch();
}
attr.setAssociation(manyManyAnnotation != null || manyOneAnnotation != null || oneManyAnnotation != null
|| oneOneAnnotation != null);
boolean lazyCollection = elemCollectionAnnotation != null
&& elemCollectionAnnotation.fetch() != FetchType.EAGER;
boolean lazyAssociation = attr.isAssociation() && (fetchType == null || fetchType == FetchType.LAZY);
attr.setLazy(lazyCollection || lazyAssociation);
} else {
attr.setDerived(true);
}
attr.setSortable(true);
attr.setFilterable(true);
return attr;
}
protected void handleMultipart(HttpServletRequest request, HttpServletResponse response, HttpHeaders headers, String store, String id, InputStream content, MediaType mimeType, String originalFilename)
throws HttpRequestMethodNotSupportedException, IOException {
ContentStoreInfo info = ContentStoreUtils.findContentStore(storeService, store);
if (info == null) {
throw new IllegalArgumentException(
String.format("Store for path %s not found", store));
}
Object domainObj = findOne(repositories, info.getDomainObjectClass(), id);
String etag = (BeanUtils.getFieldWithAnnotation(domainObj, Version.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, Version.class).toString() : null);
Object lastModifiedDate = (BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) : null);
HeaderUtils.evaluateHeaderConditions(headers, etag, lastModifiedDate);
boolean isNew = true;
if (BeanUtils.hasFieldWithAnnotation(domainObj, ContentId.class)) {
isNew = (BeanUtils.getFieldWithAnnotation(domainObj, ContentId.class) == null);
}
if (BeanUtils.hasFieldWithAnnotation(domainObj, MimeType.class)) {
BeanUtils.setFieldWithAnnotation(domainObj, MimeType.class, mimeType.toString());
}
if (originalFilename != null && StringUtils.hasText(originalFilename)) {
if (BeanUtils.hasFieldWithAnnotation(domainObj, OriginalFileName.class)) {
BeanUtils.setFieldWithAnnotation(domainObj, OriginalFileName.class, originalFilename);
}
}
domainObj = info.getImpementation().setContent(domainObj, content);
save(repositories, domainObj);
if (isNew) {
response.setStatus(HttpStatus.CREATED.value());
}
else {
response.setStatus(HttpStatus.OK.value());
}
}
protected void addBasePropertyDefinitions(MutableTypeDefinition type, Class<?> entityClazz, CmisVersion cmisVersion, boolean inherited) {
Field cmisNameField = BeanUtils.findFieldWithAnnotation(entityClazz, CmisName.class);
if (cmisNameField != null) {
type.addPropertyDefinition(this.createPropertyDefinition("cmis:name", "Name", "Name", propertyType(cmisNameField), cardinality(cmisNameField), updatability(entityClazz, cmisNameField), inherited, true, false, false));
}
if (cmisVersion != CmisVersion.CMIS_1_0) {
Field cmisDescField = BeanUtils.findFieldWithAnnotation(entityClazz, CmisDescription.class);
if (cmisDescField != null) {
type.addPropertyDefinition(this.createPropertyDefinition("cmis:description", "Description", "Description", propertyType(cmisDescField), cardinality(cmisDescField), updatability(entityClazz, cmisDescField), inherited, false, false, false));
}
}
Field idField = BeanUtils.findFieldWithAnnotation(entityClazz, Id.class);
if (idField == null) {
idField = BeanUtils.findFieldWithAnnotation(entityClazz, org.springframework.data.annotation.Id.class);
}
if (idField != null) {
type.addPropertyDefinition(this.createPropertyDefinition("cmis:objectId", "Object Id", "Object Id", PropertyType.ID, cardinality(idField), Updatability.READONLY, inherited, false, false, false));
}
type.addPropertyDefinition(this.createPropertyDefinition("cmis:baseTypeId", "Base Type Id", "Base Type Id", PropertyType.ID, Cardinality.SINGLE, Updatability.READONLY, inherited, false, false, false));
type.addPropertyDefinition(this.createPropertyDefinition("cmis:objectTypeId", "Object Type Id", "Object Type Id", PropertyType.ID, Cardinality.SINGLE, Updatability.ONCREATE, inherited, true, false, false));
if (cmisVersion != CmisVersion.CMIS_1_0) {
type.addPropertyDefinition(this.createPropertyDefinition("cmis:secondaryObjectTypeIds", "Secondary Type Ids", "Secondary Type Ids", PropertyType.ID, Cardinality.MULTI, Updatability.READONLY, inherited, false, false, false));
}
Field field = BeanUtils.findFieldWithAnnotation(entityClazz, CreatedBy.class);
if (field != null) {
type.addPropertyDefinition(this.createPropertyDefinition("cmis:createdBy", "Created By", "Created By", propertyType(field), cardinality(field), Updatability.READONLY, inherited, false, false, false));
}
field = BeanUtils.findFieldWithAnnotation(entityClazz, CreatedDate.class);
if (field != null) {
type.addPropertyDefinition(this.createPropertyDefinition("cmis:creationDate", "Creation Date", "Creation Date", propertyType(field), cardinality(field), Updatability.READONLY, inherited, false, false, false));
}
field = BeanUtils.findFieldWithAnnotation(entityClazz, LastModifiedBy.class);
if (field != null) {
type.addPropertyDefinition(this.createPropertyDefinition("cmis:lastModifiedBy", "Last Modified By", "Last Modified By", propertyType(field), cardinality(field), Updatability.READONLY, inherited, false, false, false));
}
field = BeanUtils.findFieldWithAnnotation(entityClazz, LastModifiedDate.class);
if (field != null) {
type.addPropertyDefinition(this.createPropertyDefinition("cmis:lastModificationDate", "Last Modification Date", "Last Modification Date", propertyType(field), cardinality(field), Updatability.READONLY, inherited, false, false, false));
}
field = BeanUtils.findFieldWithAnnotation(entityClazz, Version.class);
if (field != null) {
type.addPropertyDefinition(this.createPropertyDefinition("cmis:changeToken", "Change Token", "Change Token", PropertyType.STRING, Cardinality.SINGLE, Updatability.READONLY, inherited, false, false, false));
}
}
private boolean isVersionField(Field field) {
return field.getAnnotation(Version.class) != null;
}
@Override
public boolean isVersionProperty() {
return isAnnotationPresent(Version.class) || super.isVersionProperty();
}
@Version
@Column(name = "VERSION")
public Integer getVersion() {
return this.version;
}
@Version
@Column(name = "VERSION")
public Integer getVersion() {
return this.version;
}
/**
*方法: 取得java.lang.Integer
*@return: java.lang.Integer ver
*/
@Version
@Column(name="VER")
public java.lang.Integer getVer(){
return this.ver;
}
@Version
int getVersion();
@Version
@Column(name = "version", nullable = false)
public int getVersion() {
return version;
}
@Version
@Column(name = "version", nullable = false)
public int getVersion() {
return version;
}
@Version
@Column(name = "version", nullable = false)
public int getVersion() {
return version;
}
@Version
@Column(name = "version", nullable = false)
public int getVersion() {
return version;
}