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 : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
@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 : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
@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 : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
 
@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();
 
		}
	}
 
 
}