2012-04-03 59 views
2

我有一個超類休眠 - 連續映射到兩個子類

@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

我認爲這兩個線路不應該是相同的

@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的關係。

+0

謝謝!我改變了連接表,它工作。 – imgr8 2012-04-04 01:46:01

+0

你好.. – erimerturk 2012-04-04 05:28:49