Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Creation of M-M-Relation/Association fails

Hello Tom,

As the code runs within a standalone application, the entity manager is created
(emf.createEntityManager()) at the start and closed when the app terminates.
I get the transaction using em.getTransaction()
The code that persists the entities is attached (excerpt.java)

Kind Regards, Michael

Tom Ware schrieb:
> Hi Michael,
> 
>   What is the actual code you are running?  (i.e. how do you get your
> entity manager, what does the code that persists your objects look
> like?  how are you getting your transactions?)
> 
> -Tom
> 
> Michael Simons wrote:
>> Hello,
>>
>> I've got an Entity class Product with a self-relation:
>>     /** Set of products this one is compatible with.*/
>>     @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST,
>> CascadeType.REFRESH})
>>     @JoinTable(name="product_product_compatibility",
>>         joinColumns={@JoinColumn(name="prev_product_id")},
>>         inverseJoinColumns={@JoinColumn(name="next_product_id")})
>>     private Set<Product> compatibleProducts = new HashSet<Product>();
>>
>> Guess what happens when I add products to this set like this:
>>     public boolean addCompatibleProduct (Product product) {
>>         if (compatibleProducts == null)
>>             compatibleProducts = new HashSet<Product>();
>>         boolean added = compatibleProducts.add (product);
>>         if (added)
>>             product.addCompatibleProduct (this);
>>         return added;
>>     }
>>
>> If you said nothing, you've won. The only SQL that was generated is:
>> [EL Fine]: 2010-03-15
>> 15:48:09.88--ClientSession(5978600)--Connection(23637841)--Thread(Thread[main,5,main])--UPDATE
>>
>> product SET jdo_version = ? WHERE ((product_id = ?) AND (jdo_version =
>> ?))
>>     bind => [43, 30, 42]
>> [EL Fine]: 2010-03-15
>>
>> What can I do to avoid this? Or is this a bug of EclipseLink?
>>
>> Kind Regards, Michael
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> 


	/**
	 * @param matrix
	 */
	public void updateProductCompatibility (CompatibilityMx matrix) {
		for (Object o : matrix.getAlphas ()) { // Walk the lines
			ValueObject vo = (ValueObject)o;
			Product line = getEntityManager ().find (Product.class, vo.getId ());
			if (line != null) {
				Collection<Product> produits = line.getCompatibleProducts ();
				List<Product> news = new ArrayList<Product>(Math.max (produits.size (),3));
				List<Product> obsoletes = new ArrayList<Product> (Math.max (produits.size (),3));
				for (Object c : matrix.getOmegas ()) { // Walk the columns
					ValueObject po = (ValueObject)c;
					Product product = getEntityManager ().find (Product.class, po.getId ());
					if (product != null) {
						if (intraProductCompatibility) {
							// Products listed in produits _are_ compatible
							if (matrix.isCompatible (vo, po)) {
								if (produits.contains (product)) { // fine
								} else {
									// We must create an entry
									news.add (product);
								}
							} else { // !compatible
								if (produits.contains (product)) {
									// We must remove it
									obsoletes.add (product);
								} else { // fine
								}
							}
						} else {
							// Products listed in produits _are_not_ compatible
							if (matrix.isCompatible (vo, po)) {
								if (produits.contains (product)) {
									obsoletes.add (product);
								} else {
									// Fine
								}
							} else { // !compatible
								if (produits.contains (product)) {
									// fine
								} else {
									// We must remove it
									news.add (product);
								}
							}
						}
					}
				}
				line.removeCompatibleProducts (obsoletes);
				line.addCompatibleProducts (news);
			}
		}
	}
	
	

public final class CompatibilityMx {
	private Object[] 	alphas = new Object[]{};
	private Object[]	omegas = new Object[]{};
	private boolean[][] mc = new boolean[][]{};
...
}

Back to the top