Hibernate问题 - 在@JoinTable中缺少关联

时间:2016-10-11 08:55:19

标签: java spring hibernate jpa

我使用Spring和Hibernate。我有以下JPA映射:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "chg_type")
public abstract class EntitlementChange extends AbstractEntity<Long> {

    protected static List<Argument<?>> copy(List<Argument<?>> arguments) {
        return arguments.stream()
            .map(Argument::getCopy)
            .collect(toList());
    }

    protected EntitlementChange(List<Argument<?>> arguments) {
        super();

        this.arguments = copy(arguments);
    }

    @Id
    @GeneratedValue(generator = SequenceOf.ENTITLEMENT_CHANGE_IDS)
    @SequenceGenerator(name = SequenceOf.ENTITLEMENT_CHANGE_IDS, sequenceName = SequenceOf.ENTITLEMENT_CHANGE_IDS, allocationSize = 50)
    private Long id;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable
    private List<Argument<?>> arguments;
}

继承自EntitlementChange类的实体。

@Entity
@DiscriminatorValue(EntitlementModification.DISCRIMINATOR)
public class EntitlementModification extends EntitlementChange {

    public static final String DISCRIMINATOR = "modify";

    public EntitlementModification(List<Argument<?> arguments) {
        super(arguments);
    }
}

问题在于,当我使用参数列表创建新的EntitlementModification对象并使用事务将其保存到数据库时:

@Transactional
public EntitlementChange createEntitlementChange(List<Argument<?>> arguments) {

    EntitlementChange change = new EntitlementModification(arguments);

    return entitlementChangeRepository.save(change);
}

我获得带有新ID的EntitlementChange对象,其中包含带有已分配ID的参数列表。当我们查看数据库时,我们可以看到创建了参数条目,创建了EntitlementChange条目,但是没有创建连接表中的关联。因此,当我使用存储库获取EntitlementChange对象时,参数为null。参数类:

@Entity
@DiscriminatorColumn(name = "arg_type", discriminatorType = DiscriminatorType.STRING)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@JsonTypeInfo(include = As.PROPERTY, property = "type", use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({
        @Type(value = LongStringArgument.class, name = LongStringArgument.DISCRIMINATOR),
        @Type(value = StringArgument.class, name = StringArgument.DISCRIMINATOR),
        @Type(value = BooleanArgument.class, name = BooleanArgument.DISCRIMINATOR),
        @Type(value = SelectOneArgument.class, name = SelectOneArgument.DISCRIMINATOR),
        @Type(value = MultiSelectArgument.class, name = MultiSelectArgument.DISCRIMINATOR),
        @Type(value = IntegerArgument.class, name = IntegerArgument.DISCRIMINATOR)
})
public abstract class Argument<T> extends AbstractEntity<Long> {

    @Id
    @GeneratedValue(generator = SequenceOf.ARGUMENT_IDS)
    @SequenceGenerator(name = SequenceOf.ARGUMENT_IDS, sequenceName = SequenceOf.ARGUMENT_IDS, allocationSize = 50)
    private Long id;

    private String name;

    protected Argument() {
        super();
    }

    public Argument(String name) {
        super();

        this.name = name;
    }

    @Override
    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public abstract T getValue();

    @JsonIgnore
    public abstract Argument<T> getCopy();

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;

        if (!(o instanceof Argument))
            return false;

        Argument<?> argument = (Argument<?>) o;

        return new EqualsBuilder()
                .append(getName(), argument.getName())
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(getName())
                .toHashCode();
    }
}

这种奇怪行为可能是什么原因?

0 个答案:

没有答案