Bonjour,
Soit AMDL et BMDL (des pojos gérés par JPA/Hibernate) reliés de la façon suivante :
AMDL <-1---*-> BMDL
le owning side est donc BMDL
Dans mon service géré par Spring si je veux créer un AMDL et un BMDL rattaché suffit-il de faire :
<<
AMDL aMDL = new AMDL();
BMDL bMDL = new BMDL();
bMDL.setAmdl(aMDL);
bDAO.create(bMDL);
>>
ou faut il absolument faire
<<
AMDL aMDL = new AMDL();
BMDL bMDL = new BMDL();
bMDL.setAmdl(aMDL);
aMDL.getBmdls().add(bMDL);
bDAO.create(bMDL);
>> ?
Cordialement,
Karl A.
---
Code:
@Entity
@Table(name="AMDL")
public class AMDL {
private Long id;
private long version;
private String value;
private Set<BMDL> bmdls = new HashSet<BMDL>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
@Column(name="VERSION", nullable=false)
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
@Column(name="VALUE", nullable=true)
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@OneToMany(mappedBy="amdl", cascade=CascadeType.ALL)
public Set<BMDL> getBmdls() {
return bmdls;
}
public void setBmdls(Set<BMDL> bmdls) {
this.bmdls = bmdls;
}
Code:
@Entity
@Table(name="BMDL")
public class BMDL {
private Long id;
private long version;
private String value;
private AMDL amdl;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
@Column(name="VERSION", nullable=false)
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
@Column(name="VALUE", nullable=true)
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="AMDL_ID")
public AMDL getAmdl() {
return amdl;
}
public void setAmdl(AMDL amdl) {
this.amdl = amdl;
}
Code:
@Repository("BDAO")
public class BDAO implements IBDAO
{
private static final Log LOG = LogFactory.getLog(BDAO.class);
@PersistenceContext
private EntityManager entityManager;
//~------------------------------------------------------------------------
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IADAO#persist(com.clog.workey.data.model.AMDL)
*/
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IBDAO#persist(com.clog.workey.data.model.BMDL)
*/
@Override
public void persist(BMDL transientBMDL)
{ if (LOG.isDebugEnabled()) LOG.debug("persisting AMDL instance");
try
{ entityManager.persist(transientBMDL);
if (LOG.isDebugEnabled()) LOG.debug("persist successful");
}
catch (RuntimeException re)
{ LOG.error("persist failed", re); }
entityManager.flush();
}
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IADAO#create(com.clog.workey.data.model.AMDL)
*/
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IBDAO#create(com.clog.workey.data.model.BMDL)
*/
@Override
public BMDL create(BMDL aObj) {
//System.out.println("transaction used : " + entityManager.getTransaction().toString());
persist(aObj);
entityManager.flush();
return aObj;
}
//~------------------------------------------------------------------------
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IADAO#remove(com.clog.workey.data.model.AMDL)
*/
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IBDAO#remove(com.clog.workey.data.model.BMDL)
*/
@Override
public void remove(BMDL persistentBMDL)
{ if (LOG.isDebugEnabled()) LOG.debug("removing AMDL instance");
try
{ entityManager.remove(persistentBMDL);
if (LOG.isDebugEnabled()) LOG.debug("remove successful");
}
catch (RuntimeException re)
{ LOG.error("remove failed", re); }
finally {
entityManager.flush();
}
}
//~------------------------------------------------------------------------
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IADAO#remove(java.lang.Object)
*/
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IBDAO#remove(java.lang.Object)
*/
@Override
public void remove(Object oPK)
{ this.remove(this.findByPK(oPK));
entityManager.flush();}
//~------------------------------------------------------------------------
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IADAO#merge(com.clog.workey.data.model.AMDL)
*/
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IBDAO#merge(com.clog.workey.data.model.BMDL)
*/
@Override
public BMDL merge(BMDL detachedBMDL)
{ if (LOG.isDebugEnabled()) LOG.debug("merging Actors instance");
try
{ BMDL result = entityManager.merge(detachedBMDL);
if (LOG.isDebugEnabled()) LOG.debug("merge successful");
return(result);
}
catch (RuntimeException re)
{ LOG.error("merge failed", re);
return(null);
}
finally {
entityManager.flush();
}
}
//~------------------------------------------------------------------------
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IADAO#findByPK(java.lang.Object)
*/
/* (non-Javadoc)
* @see com.clog.workey.data.dao.IBDAO#findByPK(java.lang.Object)
*/
@Override
public BMDL findByPK(Object oPK)
{
if (LOG.isDebugEnabled()) LOG.debug("getting BMDL instance with id: " + oPK);
try
{
//System.out.println("transaction used : " + entityManager.getTransaction().toString());
BMDL instance = entityManager.find(BMDL.class, oPK);
if (LOG.isDebugEnabled()) LOG.debug("findByPK successful");
return(instance);
}
catch (RuntimeException re)
{ LOG.error("findById failed", re);
return(null);
}
finally {
entityManager.flush();
}
}
}