2012-03-07 3 views
0

私はしばらく私の頭を掻いていて、助けを得ると思った。Hibernateが2つのセッションを作成する理由を理解できませんか?

私は変更できないレガシーデータベースを使っている。私はゲッター/セッターを示す除外した簡潔にするために

@Entity 
public class Institution { 
    @Id 
    private Long id; 

    @OneToMany(mappedBy="institution", fetch=FetchType.EAGER) 
    private List<Subscription> subscriptions = new ArrayList<Subscription>(); 
} 

@Entity 
public class Subscription { 
    @Id 
    private Long id; 

    @ManyToOne 
    @JoinColumn(name="sub_id", referencedColumnName="ins_sub_id", insertable=false, updatable=false) 
    private Institution institution; 
} 

:私は、次のドメインを持っています。結合テーブルはありません。

So;

1)マッピングは正しいですか?私は双方向の関連付けを望んでおり、私はその機関を関係の所有者にしたい。

2)私は、機関をロードnew Subscription()を作成し、subscriptionsコレクションにサブスクリプションを追加する場合...

@RequestMapping(value="/add/{institutionId}", method=RequestMethod.POST) 
public String submitSubscriptionForm(@ModelAttribute SubscriptionForm form) { 

    Institution institution = institutionService.getById(form.getInstitutionId()); 
    Subscription subscription = new Subscription(); 
    //...set properties on subscripton from data in the form 
    subscription.setInstitution(institution); 
    institution.getSubscriptions().add(subscription); 
    institutionService.saveOrUpdateInstitution(institution); 
} 

...私は機関を救う...

institutionService.saveOrUpdateInstitution(institution);だけHibernateDaoSupportを拡張するDAOに代理人を送ります。

...私は次のエラーを取得:

org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions 
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679) 
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) 
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) 
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) 
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:737) 
at com.f1000.dao.hibernate.InstitutionDaoImpl.saveOrUpdate(InstitutionDaoImpl.java:161) 

を私は春を使用していますし、OpenSessionInViewFilterを利用しています第二セッションが作成された理由を私は理解できませんか?あなたの関連した問題があり

答えて

1

@ManyToOne 
    @JoinColumn(name="sub_id", referencedColumnName="ins_sub_id") 
    private Institution institution; 

ザ・はSubscription中にあなたinstitutioninsertable=false, updatable=falseセットです。新しいサブスクリプションに対して、それを削除するか、新しいサブプロパティを作成する必要があります。 insertable=false, updatable=falseマッピングの詳細について

private Long institutionId; 

と置き換えるsubscription.setInstitution(institution);することにより、この、

subscription.setInstitutionId(institution.getId()); 

読むhere

関連する問題