在连接表JPA中映射额外属性

时间:2014-03-30 15:08:47

标签: java jpa many-to-many appfuse

我需要映射两个具有ManyToMany关系的类,但关系连接表有其他数据。

我试图在两周前通过此链接建立此关系模型:http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

我的插入测试(testAddAndRemoveDroitAbsence)工作正常,但是当我尝试更新(testUpdateDroitAbsence)时,我得到了这个例外:

  org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find c
  om.programanagement.model.DroitAbsenceUser with id com.programanagement.model.Dr
  oitUserId@9cf670[
   userId=-1
   droitAbsenceId=-1
 ]; nested exception is javax.persistence.EntityNotFoundException: Unable to find
  com.programanagement.model.DroitAbsenceUser with id com.programanagement.model.
  DroitUserId@9cf670[
  userId=-1
  droitAbsenceId=-1
]

你有什么想要帮我的吗?

PS:这些是我的实体类:

public class DroitAbsenceDaoTest  extends BaseDaoTestCase {
@PersistenceContext
private EntityManager entityManager;

@Autowired
private DroitAbsenceDao dao;
@Autowired
private TypeAbsenceDao tadao;

@Autowired
private UserDao udao;

@Test
//@ExpectedException(DataIntegrityViolationException.class)
public void testUpdateDroitAbsence() throws Exception {
    DroitAbsence lDroitAbsence = dao.get(-1L);

    lDroitAbsence.setNbOfDays("20");
    lDroitAbsence.addUser(udao.get(-1L), "13","test"); 
    dao.save(lDroitAbsence);

    lDroitAbsence = dao.get(-1L);
    assertEquals(lDroitAbsence.getNbOfDays(), "20");

    // should throw DataIntegrityViolationException
    dao.save(lDroitAbsence);
}


@Test
@ExpectedException(ObjectRetrievalFailureException.class)
public void testAddAndRemoveDroitAbsence() throws Exception {
    DroitAbsence lDroitAbsence = new DroitAbsence();
    Date date = new Date(System.currentTimeMillis());
    lDroitAbsence.setDateDebut(date);
    lDroitAbsence.setNbOfDays("12");
    TypeAbsence lTypeAbsence = tadao.get(2L);
    lDroitAbsence.setTypeAbsence(lTypeAbsence);
    lDroitAbsence.addUser(udao.get(-1L), "13","test");        
    lDroitAbsence = dao.save(lDroitAbsence);

    assertNotNull(lDroitAbsence.getId());
    assertEquals("12", lDroitAbsence.getNbOfDays());
    assertEquals(1, lDroitAbsence.getUsers().size());

    dao.remove(lDroitAbsence.getId());

    //should throw EntityNotFoundException
    dao.get(lDroitAbsence.getId());
}


} 

 @Entity
 @Table(name = "app_user")
 @Indexed
 @XmlRootElement
 public class User extends BaseObject implements Serializable, UserDetails {
 private static final long serialVersionUID = 3832626162173359411L;
 private Long id;
 private Integer version;
 private List<DroitAbsenceUser> droitAbsences = new ArrayList<DroitAbsenceUser>();

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @DocumentId
 public Long getId() {
    return id;
 }
 @OneToMany(mappedBy = "user",fetch = FetchType.EAGER)
 public List <DroitAbsenceUser> getDroitAbsences() {
    return droitAbsences;
}

}

@Entity
@Table(name = "droit_absence")
@Indexed
@XmlRootElement
public class DroitAbsence extends BaseObject implements Serializable {
private static final long serialVersionUID = 3617859655440969541L;

private Long   id;

private String nbOfDays;        // default value required 
private Date dateDebut;         // required
private Date dateFin;           // required
private TypeAbsence typeAbsence;// required, unique
private Integer version;

private List<DroitAbsenceUser> users = new ArrayList<DroitAbsenceUser>();

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@DocumentId
public Long getId() {
      return id;
 }

 @ManyToOne
 @JoinColumn(name="typeAbsence_id", unique = true,nullable = false)
  public TypeAbsence getTypeAbsence() {
    return typeAbsence;
  }

 @OneToMany(mappedBy = "droitAbsence", fetch = FetchType.EAGER)
 public List<DroitAbsenceUser> getUsers() {
    return users;
 }

  public void addUser(User user, String nb, String comm) {
        DroitAbsenceUser association = new DroitAbsenceUser();
        association.setUser(user);
        association.setDroitAbsence(this);
        association.setUserId(user.getId());
        association.setDroitAbsenceId(this.getId());
        association.setNbOfDays(nb);
        association.setCommentaire(comm); 
        users.add(association);
        // Also add the association object to the user.
        user.getDroitAbsences().add(association);
}

//getters and setters...

}


public class DroitUserId implements Serializable { 
private static final long serialVersionUID = 3617855855441969541L;
private Long userId;
private Long droitAbsenceId;

//getters and setters...
}

La classe droitAbsenceUser:

 @Entity
 @Table(name = "droit_user")
 @IdClass(DroitUserId.class)
 @Indexed
 @XmlRootElement
 //@AssociationOverrides({
 //@AssociationOverride(name ="pk.user", joinColumns = @JoinColumn(name ="user_id")),
 //@AssociationOverride(name ="pk.droitAbsence", joinColumns = @JoinColumn(name        ="droitAbsence_id"))
 //        })
public class DroitAbsenceUser extends BaseObject implements Serializable {
private static final long serialVersionUID = 3617859855441969541L;

private Long userId;
private Long droitAbsenceId;

private DroitAbsence droitAbsence;
private User user;   
private String nbOfDays;        // required
private String commentaire;

@Id         
public Long getUserId() {
      return userId;
 }
public void setUserId(Long userId) {
    this.userId = userId;
}
@Id   
public Long getDroitAbsenceId() {
      return droitAbsenceId;
 }

 @ManyToOne
 //@PrimaryKeyJoinColumn(name="droitAbsenceId",         //referencedColumnName="ID")
 @JoinColumn(name="droitAbsenceId",  updatable = false, insertable = false)
 //@NotFound(action=NotFoundAction.IGNORE)
 public DroitAbsence getDroitAbsence() {
    return droitAbsence;
}

  @ManyToOne
 @JoinColumn(name="userId", updatable = false, insertable = false)
 //@NotFound(action=NotFoundAction.IGNORE)
 //@PrimaryKeyJoinColumn(name="userId", referencedColumnName="ID")
 public User getUser() {
    return user;
 }
  //getters and setters...
 }



org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find c
om.programanagement.model.DroitAbsenceUser with id com.programanagement.model.Dr
oitUserId@9cf670[
userId=-1
droitAbsenceId=-1
]; nested exception is javax.persistence.EntityNotFoundException: Unable to find
com.programanagement.model.DroitAbsenceUser with id com.programanagement.model.
 DroitUserId@9cf670[
userId=-1
droitAbsenceId=-1
]
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAcces
sExceptionIfPossible(EntityManagerFactoryUtils.java:301)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translat
 eExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator
.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(
  DataAccessUtils.java:213)

  Caused by: javax.persistence.EntityNotFoundException: Unable to find com.program
  anagement.model.DroitAbsenceUser with id com.programanagement.model.DroitUserId@
  9cf670[
  userId=-1
  droitAbsenceId=-1
  ]

由于

0 个答案:

没有答案