사용하기 쉬운
Springboot 프로젝트에 jpa 도입
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
공개 필드 정의
import javax.persistence.*;
import java.util.Date;
@MappedSuperclass //告诉 jpa 这是公用字段
public class CommonModel {
@Id
@Column(columnDefinition = "bigint(64) COMMENT 'ID'")
private Long id;
@Column(columnDefinition = "varchar(32) COMMENT '创建者'")
private String createBy;
@Column(columnDefinition = "datetime COMMENT '创建时间'")
private Date createTime;
@Column(columnDefinition = "datetime COMMENT '更新者'")
private String updateBy;
@Column(columnDefinition = "datetime COMMENT '更新时间'")
private Date updateTime;
@Column(columnDefinition = "datetime COMMENT '备注'")
private String remark;
}
테이블 필드 정의
import lombok.Data;
import javax.persistence.*;
@Entity
@Table(name = "data_catalog")
@Data
@org.hibernate.annotations.Table(appliesTo = "data_catalog",comment="数据目录") //告诉数据库这是表注释
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class DataCatalog extends CommonModel {
@Column(columnDefinition = "varchar(32) COMMENT '目录名称'")
private String name; // 目录名称
@Column(columnDefinition = "bigint(64) DEFAULT 0 COMMENT '上级目录'")
private Long parentId; // 上级目录
@Column(columnDefinition = "varchar(32) COMMENT '目录描述'")
private String description; // 目录描述
@Column(columnDefinition = "int COMMENT '排序'")
private Integer sort; // 排序
}
위의 단계를 통해 자동 테이블 생성을 쉽게 실현할 수 있습니다!
물론 설정 파일에 spring.jpa.hibernate.ddl-auto = update 를 설정하는 것을 잊지 마시고
정렬 문제에 대해서는 아래에서 말씀드리겠습니다.
정렬하다
jpa의 정렬 논리를 다시 작성해야 합니다.
여기서 우리는 java 디렉토리에 직접 두 개의 새로운 클래스를 생성합니다.
원칙은 최종 필드 속성 반환 값을 수정하여 재정렬을 달성하는 것입니다.
이 등록은 클래스 이름과 정확히 동일함을 기억하십시오. 아래에!
이것은 클래스 파일을 로드하기 위해 jvm의 원리를 사용하여 jap의 소스 코드를 다루기 위한 것입니다. jar 패키지에서 클래스를 이동할 필요가 없습니다!
상속 상태 코드
이 부분을 수정하기 위한 코드이며 속성의 순서가 변경되었습니다.
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.hibernate.cfg;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.Access;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import org.hibernate.AnnotationException;
import org.hibernate.annotations.common.reflection.XAnnotatedElement;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.cfg.annotations.EntityBinder;
import org.hibernate.mapping.PersistentClass;
public class InheritanceState {
private XClass clazz;
private boolean hasSiblings = false;
private boolean hasParents = false;
private InheritanceType type;
private boolean isEmbeddableSuperclass = false;
private Map<XClass, InheritanceState> inheritanceStatePerClass;
private List<XClass> classesToProcessForMappedSuperclass = new ArrayList();
private MetadataBuildingContext buildingContext;
private AccessType accessType;
private InheritanceState.ElementsToProcess elementsToProcess;
private Boolean hasIdClassOrEmbeddedId;
public InheritanceState(XClass clazz, Map<XClass, InheritanceState> inheritanceStatePerClass, MetadataBuildingContext buildingContext) {
this.setClazz(clazz);
this.buildingContext = buildingContext;
this.inheritanceStatePerClass = inheritanceStatePerClass;
this.extractInheritanceType();
}
private void extractInheritanceType() {
XAnnotatedElement element = this.getClazz();
Inheritance inhAnn = (Inheritance)element.getAnnotation(Inheritance.class);
MappedSuperclass mappedSuperClass = (MappedSuperclass)element.getAnnotation(MappedSuperclass.class);
if (mappedSuperClass != null) {
this.setEmbeddableSuperclass(true);
this.setType(inhAnn == null ? null : inhAnn.strategy());
} else {
this.setType(inhAnn == null ? InheritanceType.SINGLE_TABLE : inhAnn.strategy());
}
}
boolean hasTable() {
return !this.hasParents() || !InheritanceType.SINGLE_TABLE.equals(this.getType());
}
boolean hasDenormalizedTable() {
return this.hasParents() && InheritanceType.TABLE_PER_CLASS.equals(this.getType());
}
public static InheritanceState getInheritanceStateOfSuperEntity(XClass clazz, Map<XClass, InheritanceState> states) {
XClass superclass = clazz;
do {
superclass = superclass.getSuperclass();
InheritanceState currentState = (InheritanceState)states.get(superclass);
if (currentState != null && !currentState.isEmbeddableSuperclass()) {
return currentState;
}
} while(superclass != null && !Object.class.getName().equals(superclass.getName()));
return null;
}
public static InheritanceState getSuperclassInheritanceState(XClass clazz, Map<XClass, InheritanceState> states) {
XClass superclass = clazz;
do {
superclass = superclass.getSuperclass();
InheritanceState currentState = (InheritanceState)states.get(superclass);
if (currentState != null) {
return currentState;
}
} while(superclass != null && !Object.class.getName().equals(superclass.getName()));
return null;
}
public XClass getClazz() {
return this.clazz;
}
public void setClazz(XClass clazz) {
this.clazz = clazz;
}
public boolean hasSiblings() {
return this.hasSiblings;
}
public void setHasSiblings(boolean hasSiblings) {
this.hasSiblings = hasSiblings;
}
public boolean hasParents() {
return this.hasParents;
}
public void setHasParents(boolean hasParents) {
this.hasParents = hasParents;
}
public InheritanceType getType() {
return this.type;
}
public void setType(InheritanceType type) {
this.type = type;
}
public boolean isEmbeddableSuperclass() {
return this.isEmbeddableSuperclass;
}
public void setEmbeddableSuperclass(boolean embeddableSuperclass) {
this.isEmbeddableSuperclass = embeddableSuperclass;
}
void postProcess(PersistentClass persistenceClass, EntityBinder entityBinder) {
this.getElementsToProcess();
this.addMappedSuperClassInMetadata(persistenceClass);
entityBinder.setPropertyAccessType(this.accessType);
}
public XClass getClassWithIdClass(boolean evenIfSubclass) {
if (!evenIfSubclass && this.hasParents()) {
return null;
} else if (this.clazz.isAnnotationPresent(IdClass.class)) {
return this.clazz;
} else {
InheritanceState state = getSuperclassInheritanceState(this.clazz, this.inheritanceStatePerClass);
return state != null ? state.getClassWithIdClass(true) : null;
}
}
public Boolean hasIdClassOrEmbeddedId() {
if (this.hasIdClassOrEmbeddedId == null) {
this.hasIdClassOrEmbeddedId = false;
if (this.getClassWithIdClass(true) != null) {
this.hasIdClassOrEmbeddedId = true;
} else {
InheritanceState.ElementsToProcess process = this.getElementsToProcess();
Iterator var2 = process.getElements().iterator();
while(var2.hasNext()) {
PropertyData property = (PropertyData)var2.next();
if (property.getProperty().isAnnotationPresent(EmbeddedId.class)) {
this.hasIdClassOrEmbeddedId = true;
break;
}
}
}
}
return this.hasIdClassOrEmbeddedId;
}
public InheritanceState.ElementsToProcess getElementsToProcess() {
if (this.elementsToProcess == null) {
InheritanceState inheritanceState = (InheritanceState)this.inheritanceStatePerClass.get(this.clazz);
assert !inheritanceState.isEmbeddableSuperclass();
this.getMappedSuperclassesTillNextEntityOrdered();
this.accessType = this.determineDefaultAccessType();
ArrayList<PropertyData> elements = new ArrayList();
int idPropertyCount = 0;
int currentIdPropertyCount;
for(Iterator var4 = this.classesToProcessForMappedSuperclass.iterator(); var4.hasNext(); idPropertyCount += currentIdPropertyCount) {
XClass classToProcessForMappedSuperclass = (XClass)var4.next();
PropertyContainer propertyContainer = new PropertyContainer(classToProcessForMappedSuperclass, this.clazz, this.accessType);
currentIdPropertyCount = AnnotationBinder.addElementsOfClass(elements, propertyContainer, this.buildingContext);
}
if (idPropertyCount == 0 && !inheritanceState.hasParents()) {
throw new AnnotationException("No identifier specified for entity: " + this.clazz.getName());
}
elements.trimToSize();
this.elementsToProcess = new InheritanceState.ElementsToProcess(elements, idPropertyCount);
}
return this.elementsToProcess;
}
private AccessType determineDefaultAccessType() {
XClass xclass;
for(xclass = this.clazz; xclass != null; xclass = xclass.getSuperclass()) {
if ((xclass.getSuperclass() == null || Object.class.getName().equals(xclass.getSuperclass().getName())) && (xclass.isAnnotationPresent(Entity.class) || xclass.isAnnotationPresent(MappedSuperclass.class)) && xclass.isAnnotationPresent(Access.class)) {
return AccessType.getAccessStrategy(((Access)xclass.getAnnotation(Access.class)).value());
}
}
label62:
for(xclass = this.clazz; xclass != null && !Object.class.getName().equals(xclass.getName()); xclass = xclass.getSuperclass()) {
if (xclass.isAnnotationPresent(Entity.class) || xclass.isAnnotationPresent(MappedSuperclass.class)) {
Iterator var2 = xclass.getDeclaredProperties(AccessType.PROPERTY.getType()).iterator();
XProperty prop;
boolean isEmbeddedId;
do {
if (!var2.hasNext()) {
var2 = xclass.getDeclaredProperties(AccessType.FIELD.getType()).iterator();
do {
if (!var2.hasNext()) {
continue label62;
}
prop = (XProperty)var2.next();
isEmbeddedId = prop.isAnnotationPresent(EmbeddedId.class);
} while(!prop.isAnnotationPresent(Id.class) && !isEmbeddedId);
return AccessType.FIELD;
}
prop = (XProperty)var2.next();
isEmbeddedId = prop.isAnnotationPresent(EmbeddedId.class);
} while(!prop.isAnnotationPresent(Id.class) && !isEmbeddedId);
return AccessType.PROPERTY;
}
}
throw new AnnotationException("No identifier specified for entity: " + this.clazz);
}
private void getMappedSuperclassesTillNextEntityOrdered() {
XClass currentClassInHierarchy = this.clazz;
InheritanceState superclassState;
do {
this.classesToProcessForMappedSuperclass.add(0, currentClassInHierarchy);
XClass superClass = currentClassInHierarchy;
do {
superClass = superClass.getSuperclass();
superclassState = (InheritanceState)this.inheritanceStatePerClass.get(superClass);
} while(superClass != null && !this.buildingContext.getBuildingOptions().getReflectionManager().equals(superClass, Object.class) && superclassState == null);
currentClassInHierarchy = superClass;
} while(superclassState != null && superclassState.isEmbeddableSuperclass());
}
private void addMappedSuperClassInMetadata(PersistentClass persistentClass) {
org.hibernate.mapping.MappedSuperclass mappedSuperclass = null;
InheritanceState superEntityState = getInheritanceStateOfSuperEntity(this.clazz, this.inheritanceStatePerClass);
PersistentClass superEntity = superEntityState != null ? this.buildingContext.getMetadataCollector().getEntityBinding(superEntityState.getClazz().getName()) : null;
int lastMappedSuperclass = this.classesToProcessForMappedSuperclass.size() - 1;
for(int index = 0; index < lastMappedSuperclass; ++index) {
org.hibernate.mapping.MappedSuperclass parentSuperclass = mappedSuperclass;
Class<?> type = this.buildingContext.getBuildingOptions().getReflectionManager().toClass((XClass)this.classesToProcessForMappedSuperclass.get(index));
mappedSuperclass = this.buildingContext.getMetadataCollector().getMappedSuperclass(type);
if (mappedSuperclass == null) {
mappedSuperclass = new org.hibernate.mapping.MappedSuperclass(parentSuperclass, superEntity);
mappedSuperclass.setMappedClass(type);
this.buildingContext.getMetadataCollector().addMappedSuperclass(type, mappedSuperclass);
}
}
if (mappedSuperclass != null) {
persistentClass.setSuperMappedSuperclass(mappedSuperclass);
}
}
static final class ElementsToProcess {
private final List<PropertyData> properties;
private final int idPropertyCount;
public List<PropertyData> getElements() {
return this.properties;
}
public int getIdPropertyCount() {
return this.idPropertyCount;
}
private ElementsToProcess(List<PropertyData> properties, int idPropertyCount) {
List<PropertyData> perList = new ArrayList<>();
List<PropertyData> finalList = new ArrayList<>();
properties.forEach(item->{
XClass last = properties.get(properties.size()-1).getDeclaringClass();
String name = item.getProperty().getName();
if(item.getDeclaringClass()!=last && !name.equals("id") ){
finalList.add(item);
}else{
perList.add(item);
}
});
perList.addAll(finalList);
this.properties = perList;
this.idPropertyCount = idPropertyCount;
}
}
}
PropertyContainer 코드
코드의 이 부분은 속성 저장소 개체의 유형을 변경하고 Map을 LinkedHashMap으로 변경하여 질서를 달성하는 것입니다!
package org.hibernate.cfg;
import org.hibernate.AnnotationException;
import org.hibernate.annotations.Any;
import org.hibernate.annotations.ManyToAny;
import org.hibernate.annotations.Target;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.boot.MappingException;
import org.hibernate.boot.jaxb.Origin;
import org.hibernate.boot.jaxb.SourceType;
import org.hibernate.cfg.annotations.HCANNHelper;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.jboss.logging.Logger;
import javax.persistence.*;
import java.util.*;
/**
* @Author: 凤凰[小哥哥]
* @Date: 2019/9/20 16:12
* @Email: [email protected]
*/
public class PropertyContainer {
private static final CoreMessageLogger LOG = (CoreMessageLogger) Logger.getMessageLogger(CoreMessageLogger.class, PropertyContainer.class.getName());
private final XClass xClass;
private final XClass entityAtStake;
private final AccessType classLevelAccessType;
private final LinkedHashMap<String, XProperty> persistentAttributeMap;
PropertyContainer(XClass clazz, XClass entityAtStake, AccessType defaultClassLevelAccessType) {
this.xClass = clazz;
this.entityAtStake = entityAtStake;
if (defaultClassLevelAccessType == AccessType.DEFAULT) {
defaultClassLevelAccessType = AccessType.PROPERTY;
}
AccessType localClassLevelAccessType = this.determineLocalClassDefinedAccessStrategy();
assert localClassLevelAccessType != null;
this.classLevelAccessType = localClassLevelAccessType != AccessType.DEFAULT ? localClassLevelAccessType : defaultClassLevelAccessType;
assert this.classLevelAccessType == AccessType.FIELD || this.classLevelAccessType == AccessType.PROPERTY;
this.persistentAttributeMap = new LinkedHashMap();
List<XProperty> fields = this.xClass.getDeclaredProperties(AccessType.FIELD.getType());
List<XProperty> getters = this.xClass.getDeclaredProperties(AccessType.PROPERTY.getType());
this.preFilter(fields, getters);
Map<String, XProperty> persistentAttributesFromGetters = new HashMap();
this.collectPersistentAttributesUsingLocalAccessType(this.persistentAttributeMap, persistentAttributesFromGetters, fields, getters);
this.collectPersistentAttributesUsingClassLevelAccessType(this.persistentAttributeMap, persistentAttributesFromGetters, fields, getters);
int i=0;
}
private void preFilter(List<XProperty> fields, List<XProperty> getters) {
Iterator propertyIterator = fields.iterator();
XProperty property;
while (propertyIterator.hasNext()) {
property = (XProperty) propertyIterator.next();
if (mustBeSkipped(property)) {
propertyIterator.remove();
}
}
propertyIterator = getters.iterator();
while (propertyIterator.hasNext()) {
property = (XProperty) propertyIterator.next();
if (mustBeSkipped(property)) {
propertyIterator.remove();
}
}
}
private void collectPersistentAttributesUsingLocalAccessType(LinkedHashMap<String, XProperty> persistentAttributeMap, Map<String, XProperty> persistentAttributesFromGetters, List<XProperty> fields, List<XProperty> getters) {
Iterator propertyIterator = fields.iterator();
XProperty xProperty;
Access localAccessAnnotation;
while (propertyIterator.hasNext()) {
xProperty = (XProperty) propertyIterator.next();
localAccessAnnotation = (Access) xProperty.getAnnotation(Access.class);
if (localAccessAnnotation != null && localAccessAnnotation.value() == javax.persistence.AccessType.FIELD) {
propertyIterator.remove();
persistentAttributeMap.put(xProperty.getName(), xProperty);
}
}
propertyIterator = getters.iterator();
while (propertyIterator.hasNext()) {
xProperty = (XProperty) propertyIterator.next();
localAccessAnnotation = (Access) xProperty.getAnnotation(Access.class);
if (localAccessAnnotation != null && localAccessAnnotation.value() == javax.persistence.AccessType.PROPERTY) {
propertyIterator.remove();
String name = xProperty.getName();
XProperty previous = (XProperty) persistentAttributesFromGetters.get(name);
if (previous != null) {
throw new MappingException(LOG.ambiguousPropertyMethods(this.xClass.getName(), HCANNHelper.annotatedElementSignature(previous), HCANNHelper.annotatedElementSignature(xProperty)), new Origin(SourceType.ANNOTATION, this.xClass.getName()));
}
persistentAttributeMap.put(name, xProperty);
persistentAttributesFromGetters.put(name, xProperty);
}
}
}
private void collectPersistentAttributesUsingClassLevelAccessType(LinkedHashMap<String, XProperty> persistentAttributeMap, Map<String, XProperty> persistentAttributesFromGetters, List<XProperty> fields, List<XProperty> getters) {
Iterator var5;
XProperty getter;
if (this.classLevelAccessType == AccessType.FIELD) {
var5 = fields.iterator();
while (var5.hasNext()) {
getter = (XProperty) var5.next();
if (!persistentAttributeMap.containsKey(getter.getName())) {
persistentAttributeMap.put(getter.getName(), getter);
}
}
} else {
var5 = getters.iterator();
while (var5.hasNext()) {
getter = (XProperty) var5.next();
String name = getter.getName();
XProperty previous = (XProperty) persistentAttributesFromGetters.get(name);
if (previous != null) {
throw new MappingException(LOG.ambiguousPropertyMethods(this.xClass.getName(), HCANNHelper.annotatedElementSignature(previous), HCANNHelper.annotatedElementSignature(getter)), new Origin(SourceType.ANNOTATION, this.xClass.getName()));
}
if (!persistentAttributeMap.containsKey(name)) {
persistentAttributeMap.put(getter.getName(), getter);
persistentAttributesFromGetters.put(name, getter);
}
}
}
}
public XClass getEntityAtStake() {
return this.entityAtStake;
}
public XClass getDeclaringClass() {
return this.xClass;
}
public AccessType getClassLevelAccessType() {
return this.classLevelAccessType;
}
public Collection<XProperty> getProperties() {
this.assertTypesAreResolvable();
return Collections.unmodifiableCollection(this.persistentAttributeMap.values());
}
private void assertTypesAreResolvable() {
Iterator var1 = this.persistentAttributeMap.values().iterator();
XProperty xProperty;
do {
if (!var1.hasNext()) {
return;
}
xProperty = (XProperty) var1.next();
} while (xProperty.isTypeResolved() || discoverTypeWithoutReflection(xProperty));
String msg = "Property " + StringHelper.qualify(this.xClass.getName(), xProperty.getName()) + " has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type";
throw new AnnotationException(msg);
}
private AccessType determineLocalClassDefinedAccessStrategy() {
AccessType hibernateDefinedAccessType = AccessType.DEFAULT;
AccessType jpaDefinedAccessType = AccessType.DEFAULT;
org.hibernate.annotations.AccessType accessType = (org.hibernate.annotations.AccessType) this.xClass.getAnnotation(org.hibernate.annotations.AccessType.class);
if (accessType != null) {
hibernateDefinedAccessType = AccessType.getAccessStrategy(accessType.value());
}
Access access = (Access) this.xClass.getAnnotation(Access.class);
if (access != null) {
jpaDefinedAccessType = AccessType.getAccessStrategy(access.value());
}
if (hibernateDefinedAccessType != AccessType.DEFAULT && jpaDefinedAccessType != AccessType.DEFAULT && hibernateDefinedAccessType != jpaDefinedAccessType) {
throw new org.hibernate.MappingException("@AccessType and @Access specified with contradicting values. Use of @Access only is recommended. ");
} else {
AccessType classDefinedAccessType;
if (hibernateDefinedAccessType != AccessType.DEFAULT) {
classDefinedAccessType = hibernateDefinedAccessType;
} else {
classDefinedAccessType = jpaDefinedAccessType;
}
return classDefinedAccessType;
}
}
private static boolean discoverTypeWithoutReflection(XProperty p) {
if (p.isAnnotationPresent(OneToOne.class) && !((OneToOne) p.getAnnotation(OneToOne.class)).targetEntity().equals(Void.TYPE)) {
return true;
} else if (p.isAnnotationPresent(OneToMany.class) && !((OneToMany) p.getAnnotation(OneToMany.class)).targetEntity().equals(Void.TYPE)) {
return true;
} else if (p.isAnnotationPresent(ManyToOne.class) && !((ManyToOne) p.getAnnotation(ManyToOne.class)).targetEntity().equals(Void.TYPE)) {
return true;
} else if (p.isAnnotationPresent(ManyToMany.class) && !((ManyToMany) p.getAnnotation(ManyToMany.class)).targetEntity().equals(Void.TYPE)) {
return true;
} else if (p.isAnnotationPresent(Any.class)) {
return true;
} else if (p.isAnnotationPresent(ManyToAny.class)) {
if (!p.isCollection() && !p.isArray()) {
throw new AnnotationException("@ManyToAny used on a non collection non array property: " + p.getName());
} else {
return true;
}
} else if (p.isAnnotationPresent(Type.class)) {
return true;
} else {
return p.isAnnotationPresent(Target.class);
}
}
private static boolean mustBeSkipped(XProperty property) {
return property.isAnnotationPresent(Transient.class) || "net.sf.cglib.transform.impl.InterceptFieldCallback".equals(property.getType().getName()) || "org.hibernate.bytecode.internal.javassist.FieldHandler".equals(property.getType().getName());
}
}
음, 마지막 공간이 제한되어 있습니다. 질문이 있으시면 저에게 개인적으로 메시지를 보내시거나 댓글 영역에서 토론하실 수 있습니다.