简体   繁体   中英

Migrating from Hibernate 3.6 to 4.2: Hibernate auto generates foreign keys

As the title suggests: I am currently migrating from Hibernate 3.6 to 4.2.

PROBLEM

After the upgrade, Hibernate started to automatically generate a foreign key that points to the same table. Now, when trying to persist a new customer, a ConstraintViolationException is thrown.

DETAILS

Here's a picture of what I mean:

在此处输入图像描述

Here is the code of the corresponding class:

@Entity
@Table(name = "customers")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Customer extends AbstractModel {

    public Customer(final Provider provider) {
        this.provider = provider;
    }

    @Required
    @ManyToOne(cascade = CascadeType.REFRESH)
    public Provider provider;

    @MaxSize(1023)
    public String note;
    public String getNote() { return note; }

    ...

}

As you can see, it extends from AbstractModel which is a @MappedSuperclass and contains the id property which serves as a primary key for all of our model classes. It's a @GeneratedValue .

It's a superclass itself. I don't know whether it's important so I am just going to include the code and schema of one of its child classes:

在此处输入图像描述

And the corresponding code:

@Entity
@Table(name="unregistered_customers")
public class UnregisteredCustomer extends Customer {

    @MaxSize(MAX_SIZE_OF_NAMES_AND_IDENTIFIERS)
    @Column(nullable = false, length = MAX_SIZE_OF_NAMES_AND_IDENTIFIERS)
    public String userName;

    @Email
    @MaxSize(MAX_SIZE_OF_EMAIL_ADDRESSES)
    @Column(unique = false, length = MAX_SIZE_OF_EMAIL_ADDRESSES)
    public String email;

    @MaxSize(MAX_SIZE_OF_NAMES_AND_IDENTIFIERS)
    @Column(length = MAX_SIZE_OF_NAMES_AND_IDENTIFIERS)
    public String mobile;

    public UnregisteredCustomer(final Provider provider) {
        super(provider);
    }

RESEARCH

I already looked into the migration guide but nothing in there seems to be related to my issue. I also looked into the Hibernate documentation especially into the section dealing with @Inheritance . Sadly, I didn't find anything concerning auto generated foreign keys.

QUESTION

How do I stop Hibernate from adding this self-referencing foreign key?

EDIT 1 As requested, the super class:

@MappedSuperclass
public abstract class AbstractModel extends AbstractBaseModel {

    @Id
    @GeneratedValue
    public Long id;

    public Long getId() {
        return id;
    }

    @Override
    public Object _key() {
        return id;
    }

}

Also, the super class of the super class:

@MappedSuperclass
public abstract class AbstractBaseModel extends GenericModel {

    public static final int MAX_SIZE_OF_NAMES_AND_IDENTIFIERS = 80;
    public static final int MAX_SIZE_OF_COMMENTS_AND_DESCRIPTIONS = 5000;
    public static final int MAX_LIST_SIZE = 30;
    public static final int MAX_SIZE_OF_EMAIL_ADDRESSES = 255;
    public static final int MAX_SIZE_OF_JSON_CONTENT_FIELDS = 65535;

    @Column(nullable = false, unique = true)
    public String uuid;

    @Column(nullable = false)
    public Long created;
    public DateTime getCreatedAsDate() {
        return DateUtil.dateTimeWithSystemTimezone(created);
    }

    @Column(nullable = false)
    public Long lastModified;


    @PrePersist
    protected void prePersist() {
        final Long nowInMillis = DateUtil.dateTimeWithSystemTimezone().getMillis();

        if (uuid == null) {
            uuid = UuidUtil.newUUIDAsString();
            Logger.trace("Created new Uuid for entity: %s", uuid);
        }
        if (created == null) {
            created =  nowInMillis;
        }
        if (lastModified == null) {
            lastModified = nowInMillis;
        }
    }

    @PreUpdate
    protected void preUpdate() {
        lastModified = DateUtil.dateTimeWithSystemTimezone().getMillis();
    }
   
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        final AbstractBaseModel other = (AbstractBaseModel) obj;
        return new EqualsBuilder().append(uuid, other.uuid).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(uuid).toHashCode();
    }

}

Put @Inheritance(strategy = InheritanceType.JOINED) to the parent entity (AbstractModel) not to the child. And customize the foreign key constraint using @PrimaryKeyJoinColumn at your child entity. Check out Part 4 (Joined Table) at https://www.baeldung.com/hibernate-inheritance to see it more clearly!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM