0

我有两个实体EquipementCompany像这样:

public class Equipement{
  String type ;
  Company company ;

  @ManyToOne(cascade = CascadeType.ALL)
  public Company getCompany(){
    return this.company ;
  }
}

public class Company{
  // fields getters setters ..
}

每个Equipement都是从 a 购买的Company,我需要创建Company然后将其注入Equipement

Session session = SessionFactory.openSession() ;
Transaction tx = session.beginTransaction() ;

Equipement equipement = new Equipement() ;
Company company = new company() ;
equipement.setCompany(company) ;
session.save(equipement) ;

tx.commit() ;
session.close() ;

这给了我一个例外

TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.marsamaroc.mmapp.domain.Company
4

1 回答 1

0

尝试

public class Company 
{
    @OneToMany(cascade = CascadeType.ALL)
    public Set<Equipement> getEquipements() { ... }
}

...
Session session = SessionFactory.openSession() ;
Transaction tx = session.beginTransaction() ;

Equipement equipement = new Equipement() ;
Company company = new company() ;
equipement.setCompany(company) ;

// Save should cascade (i.e. "equipment" should be saved too).
session.save(company) ;

tx.commit() ;
session.close() ;
...

顺便说一句,在英语中,它是“设备”,没有额外的“e”(以防万一!)。

于 2012-09-04T01:49:43.707 回答