0

I've been scratching my head for a while and thought I'd get some help :)

I'm working with a legacy database which I cannot change. I have the following domain:

@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;
}

For the sake of brevity I have excluded showing the getters/setters. There is no join table.

So;

1) Is the mapping correct? I want a bidirectional association and I want the Institution to be the owner of the relationship.

2) If I load a Institution, create a new Subscription() and add the subscription to the subscriptions collection...

@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);
}

...when I save the Institution...

institutionService.saveOrUpdateInstitution(institution); just delegates to a DAO which extends HibernateDaoSupport.

...I get the following error:

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)

I'm using the Spring and am making use of the OpenSessionInViewFilter and I can't figure out why a second session is created?

C0deAttack
  • 24,419
  • 18
  • 73
  • 81

1 Answers1

1

There is an issue in your association,

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

The is a insertable=false, updatable=false set on your institution inside the Subscription. Either you need to remove it or create a new property as below an set that, to the new Subscriptions.

private Long institutionId;

and replace subscription.setInstitution(institution); this by,

subscription.setInstitutionId(institution.getId());

Read here on more about the insertable=false, updatable=false mappings.

Community
  • 1
  • 1
ManuPK
  • 11,623
  • 10
  • 57
  • 76