1. ホーム

Spring boot jpaのフィールド障害問題を使用する

2022-02-27 08:04:25
<パス

その理由は、jpaを使ってテーブルを自動作成する際に、TreeMapを使っているので、それをLinkedHashMapの構造にしたいからです。

解決方法 パッケージ名:org.hibernate.cfg、クラス:hibernate.cfgを作成します。プロジェクト下の java フォルダーにパッケージ名:org.hibernate.cfg、クラス:PropertyContainer を作成します(パッケージ名、クラス名は同じにする必要があります)。

package org.hibernate.cfg;

import org.hibernate.AnnotationException;
import org.hibernate.annotations;
import org.hibernate.annotations;
import org.hibernate.annotations;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.common.reflection;
import org.hibernate.annotations.common.reflection;
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);
    }

    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<S
                }
                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; }
        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 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;
        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 (