解决hibernate建表字段乱序问题

在本项目中创建一个和源码类一样的包结构和一样名字的类

把源码中的所有代码复制到你建的类中 就可以对你创建的类进行修改了 修改好之后启动项目 程序走的就是你创建的类,数据库的所有字段都是和实体类排序一样的了,修改后代码如下:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.hibernate.cfg;

import java.util.*;
import javax.persistence.Access;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
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;

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate 中,Java 的基本数据类型和对应的包装类是可以互换使用的,但是在进行字段映射时,需要注意以下几点: 1. 议使用包装类,避免空指针异常:在 Java 中,基本数据类型是不能为 null 的,但是包装类可以为 null。在进行数据库操作时,如果使用基本数据类型,当数据库中对应的字段为 null 时,就会抛出空指针异常。因此议使用包装类,可以避免这种异常的出现。 2. 数据库中的字段类型要和 Java 类型一致:在进行字段映射时,需要确保数据库中的字段类型和 Java 类型一致,否则可能导致数据类型转换异常。 3. 对于布尔类型的字段,议使用包装类 Boolean:因为在数据库中,布尔类型的字段通常使用 tinyint(1) 类型表示,如果使用基本数据类型 boolean 进行映射,会导致数据类型转换异常。 4. 对于数值类型的字段,议使用包装类:因为在数据库中,数值类型的字段通常会有默认值,如果使用基本数据类型进行映射,当数据库中对应的字段为 null 时,就会使用默认值进行赋值,而这个默认值可能会影响业务逻辑的正确性。 下面是一个示例代码,演示了如何在 Hibernate 中进行字段映射: ``` @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "username") private String username; @Column(name = "age") private Integer age; @Column(name = "is_admin") private Boolean isAdmin; // getter 和 setter 方法省略 } ``` 在上面的示例中,我们使用了包装类 Integer 和 Boolean 进行字段映射,避免了空指针异常和数据类型转换异常的问题。同时,我们还指定了数据库中对应的字段名,确保了数据类型一致。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值