Discussion:
Mapping Extra Attribute in a Join Table JPA APPFUSE SPRING
khalid el hayani
2014-03-30 14:33:52 UTC
Permalink
Hi,

I need to map two classes that have a ManyToMany relationship, but the
relational join table has additional data :

USER (table)
IDFIRSTNAMELASTNAME1BobWay2SarahSmith

USER_DROIT (table)
USERID DROITABSENCEIDnbOfDays
1120 12202120

DROIT_ABSENCE(table)
EMPLOYEEIDPROJECTID TYPEABSENCE_ID
112112 222124


I am trying to model this relationship since two weeks ago following this
link : http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

My insert test (testAddAndRemoveDroitAbsence) works fine but when i try to
update (testUpdateDroitAbsence) i get this exception :

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

Do you have any idea to help me please?

PS : These are my Entity classes :

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...
}
__________________________________________________________________
@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
***@9cf670[
userId=-1
droitAbsenceId=-1
]; nested exception is javax.persistence.EntityNotFoundException: Unable to
find
com.programanagement.model.DroitAbsenceUser with id
com.programanagement.model.
***@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)
at
org.springframework.dao.support.PersistenceExceptionTranslationInterc
eptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:172)
at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynami
cAopProxy.java:204)
at com.sun.proxy.$Proxy55.save(Unknown Source)
at
com.programanagement.dao.DroitAbsenceDaoTest.testUpdateDroitAbsence(D
roitAbsenceDaoTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(Framework
Method.java:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCal
lable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMe
thod.java:41)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMet
hod.java:20)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.
java:28)
at
org.springframework.test.context.junit4.statements.RunBeforeTestMetho
dCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.ja
va:31)
at
org.springframework.test.context.junit4.statements.RunAfterTestMethod
Callbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at
org.springframework.test.context.junit4.statements.SpringRepeat.evalu
ate(SpringRepeat.java:72)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runCh
ild(SpringJUnit4ClassRunner.java:231)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runCh
ild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.
java:28)
at
org.springframework.test.context.junit4.statements.RunBeforeTestClass
Callbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.ja
va:31)
at
org.springframework.test.context.junit4.statements.RunAfterTestClassC
allbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(S
pringJUnit4ClassRunner.java:174)
at
org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provide
r.java:252)
at
org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4
Provider.java:141)
at
org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider
.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at
org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(
ReflectionUtils.java:189)
at
org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke
(ProviderFactory.java:165)
at
org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(Provi
derFactory.java:85)
at
org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(Fork
edBooter.java:115)
at
org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:
75)
Caused by: javax.persistence.EntityNotFoundException: Unable to find
com.program
anagement.model.DroitAbsenceUser with id
com.programanagement.model.DroitUserId@
9cf670[
userId=-1
droitAbsenceId=-1
]

Thanks
--
Khalid El HAYANI
(+33) 7 70 01 41 06
Loading...