Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Mysterious double insert (SOLVED, see reply)
Mysterious double insert (SOLVED, see reply) [message #912370] Thu, 13 September 2012 12:54 Go to next message
Christian Eugster is currently offline Christian EugsterFriend
Messages: 134
Registered: July 2009
Location: St. Gallen Switzerland
Senior Member
Hi,

I am facing a strange behaviour of eclipselink 2.1.3: The model consists of

AddressGroup
LinkPersonAddress
Address
AddressGroupMember

when I merge a new AddressGroupMember entity, the entity is merged two times with different ids. The merge is called only one time:

	public T merge(T entity)
	{
		if (!(entity instanceof User))
		{
			/**
			 * Achtung: Dieses if ist notwendig, weil sonst ein unendlicher Loop
			 * folgt.
			 */
			entity.setUser(User.getCurrent());
		}
		EntityManager em = getEntityManager();
		if (em != null)
		{
			try
			{
				em.getTransaction().begin();
				entity = em.merge(entity);
				em.flush();
				em.getTransaction().commit();
			}
			finally
			{
				if (em.getTransaction().isActive())
				{
					em.getTransaction().rollback();
				}
			}
		}
		return entity;
	}


I checked if the entityManager is called two times but it is not. The entity descriptor is as following:

package ch.eugster.events.persistence.model;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

import org.eclipse.persistence.annotations.Customizer;

@Entity
@Table(name = "events_address_group_member")
@AssociationOverrides({ @AssociationOverride(name = "user", joinColumns = @JoinColumn(name = "address_group_member_user_id")) })
@AttributeOverrides({ @AttributeOverride(name = "inserted", column = @Column(name = "address_group_member_inserted")),
		@AttributeOverride(name = "updated", column = @Column(name = "address_group_member_updated")),
		@AttributeOverride(name = "deleted", column = @Column(name = "address_group_member_deleted")),
		@AttributeOverride(name = "version", column = @Column(name = "address_group_member_version")) })
@Customizer(DeletedFilter.class)
public class AddressGroupMember extends AbstractEntity
{
	/**
	 * References
	 */
	@ManyToOne(optional = false)
	@JoinColumn(name = "address_group_member_address_group_id", referencedColumnName = "address_group_id")
	private AddressGroup addressGroup = null;

	@ManyToOne(optional = false)
	@JoinColumn(name = "address_group_member_address_id", referencedColumnName = "address_id")
	private Address address;

	@ManyToOne
	@JoinColumn(name = "address_group_member_pa_link_id", referencedColumnName = "pa_link_id")
	private LinkPersonAddress link = null;

	/**
	 * Data
	 */
	@Id
	@Column(name = "address_group_member_id")
	@GeneratedValue(generator = "events_address_group_member_id_seq")
	@TableGenerator(name = "events_address_group_member_id_seq", table = "events_sequence")
	private Long id;

	private AddressGroupMember()
	{
		super();
	}

	private AddressGroupMember(final AddressGroup addressGroup)
	{
		super();
		this.setAddressGroup(addressGroup);
		addressGroup.addAddressGroupMember(this);
	}

	private AddressGroupMember(final AddressGroup addressGroup, final Address address)
	{
		super();
		this.setAddressGroup(addressGroup);
		addressGroup.addAddressGroupMember(this);
		this.setParent(null, address);
	}

	private AddressGroupMember(final AddressGroup addressGroup, final LinkPersonAddress link)
	{
		super();
		this.setAddressGroup(addressGroup);
		addressGroup.addAddressGroupMember(this);
		this.setParent(link, link.getAddress());
	}

	public AddressGroupMember copy(final AddressGroup addressGroup)
	{
		AddressGroupMember copy = AddressGroupMember.newInstance(addressGroup);
		copy.setParent(this.getLink(), this.getAddress());
		return copy;
	}

	public Address getAddress()
	{
		return address;
	}

	public AddressGroup getAddressGroup()
	{
		return this.addressGroup;
	}

	@Override
	public Long getId()
	{
		return this.id;
	}

	public LinkPersonAddress getLink()
	{
		return this.link;
	}

	public void setAddressGroup(final AddressGroup addressGroup)
	{
		this.propertyChangeSupport.firePropertyChange("addressGroup", this.addressGroup,
				this.addressGroup = addressGroup);
	}

	@Override
	public void setId(final Long id)
	{
		this.propertyChangeSupport.firePropertyChange("id", this.id, this.id = id);
	}

	public void setParent(final LinkPersonAddress link, final Address address)
	{
		this.link = link;
		if (link != null)
		{
			// link.addAddressGroupMember(this);
		}
		this.address = address;
		if (address != null)
		{
			// address.addAddressGroupMember(this);
		}
	}

	public static AddressGroupMember newInstance()
	{
		return (AddressGroupMember) AbstractEntity.newInstance(new AddressGroupMember());
	}

	public static AddressGroupMember newInstance(final AddressGroup addressGroup)
	{
		return (AddressGroupMember) AbstractEntity.newInstance(new AddressGroupMember(addressGroup));
	}

	public static AddressGroupMember newInstance(final AddressGroup addressGroup, final Address address)
	{
		return (AddressGroupMember) AbstractEntity.newInstance(new AddressGroupMember(addressGroup, address));
	}

	public static AddressGroupMember newInstance(final AddressGroup addressGroup, final LinkPersonAddress link)
	{
		return (AddressGroupMember) AbstractEntity.newInstance(new AddressGroupMember(addressGroup, link));
	}

}



Has anyone any idea whatis going on here? Thank you!

[Updated on: Thu, 13 September 2012 13:21]

Report message to a moderator

Re: Mysterious double insert (SOLVED) [message #912385 is a reply to message #912370] Thu, 13 September 2012 13:20 Go to previous message
Christian Eugster is currently offline Christian EugsterFriend
Messages: 134
Registered: July 2009
Location: St. Gallen Switzerland
Senior Member
Sorry,

I added the AddressGroupMember entity to the owning AddressGroup and merged the AddressGroupMember entity instead of the AddressGroup entity.

Previous Topic:Unable to log bind parameters
Next Topic:"updatable = false" column getting updated
Goto Forum:
  


Current Time: Wed Nov 26 09:06:19 GMT 2014

Powered by FUDForum. Page generated in 0.10864 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software