Hibernate - 将行映射到两个子类中的任何一个

时间:2012-04-03 02:47:14

标签: spring hibernate inheritance

我有一个超类

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "entity_type", discriminatorType = DiscriminatorType.STRING)
@Table(name = "my_super")
public abstract class MySuper{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "super_id")
    private Long id;
}

和两个子类

@Entity
@DiscriminatorValue("sub1")
public class Sub1 extends MySuper {}

@Entity
@DiscriminatorValue("sub2")
public class Sub2 extends MySuper {}

现在如果另一个类中同时包含这两个子类,是否可以通过相同的连接表实例化其中一个 - 同一行??

例如:

@Entity
@Table(name = "caller")
public class Caller{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "caller_id")
    Long id;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "caller_super", joinColumns = @JoinColumn(name = "caller_id"), inverseJoinColumns = @JoinColumn(name = "super_id"))

    private Set<Sub1> sub1s;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinTable(name = "caller_super", joinColumns = @JoinColumn(name = "caller_id"), inverseJoinColumns = @JoinColumn(name = "super_id"))
    Sub2 sub2;
}

尝试实例化调用者对象时,我一直收到此错误:

org.springframework.orm.hibernate3.HibernateSystemException: could not set a field value by reflection setter of my.test.Caller.sub2; nested exception is org.hibernate.PropertyAccessException: could not set a field value by reflection setter of my.test.Caller.sub2
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:102)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:368)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

nested exception is org.hibernate.PropertyAccessException: could not set a field value by reflection setter

1 个答案:

答案 0 :(得分:1)

我认为这两行不应该相同

@JoinTable(name = "caller_super", joinColumns = @JoinColumn(name = "caller_id"), inverseJoinColumns = @JoinColumn(name = "super_id"))
private Set<Sub1> sub1s;

-

@JoinTable(name = "caller_super", joinColumns = @JoinColumn(name = "caller_id"), inverseJoinColumns = @JoinColumn(name = "super_id"))
private Sub2 sub2;

因为它们不是同一个实体,它们的连接表应该是不同的。对于ManyToMany关系类型为Sub1,但如果您尝试将它们放在同一个表中,则hibernate将尝试将Sub2置于sub1s。但它不是vaild。尝试更改您的联接表。对于ManyToOne关系。