Skip to main content

confusion on @ManyToMany - how to delete?

3 replies [Last post]
zambizzi
Offline
Joined: 2005-08-19

I've got a bi-directional many-to-many relationship between two entities that works fantastically for selecting & inserting but I'm having some trouble figuring out a way to delete the relationship in the join table.

I couldn't find a good example so I made a somewhat-educated guess. ;) Here are my entities:


@Entity
@Table(name="tbl_product_bundle")
public class ProductBundle implements Serializable
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@Column(nullable=false, length=50)
private String shortName;

@Column(nullable=false, length=100)
private String longName;

@Column(length=1000)
private String summary;

@Column(length=1000)
private String description;

@Transient
private String shortDescription;

@Column(nullable=false, length=50)
private String thumbnail;

@Column(nullable=false, length=50)
private String photo;

private boolean active;

@Transient
private boolean available;

@ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE})
private List configurations;
.......................
}

@Entity
@Table(name="tbl_product_config")
public class Configuration implements Serializable
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@ManyToOne
private Product product;

@ManyToMany(
mappedBy="configurations",
fetch=FetchType.LAZY,
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
private List productBundles;

@Column(nullable=false, length=50)
private String description;

@Column(nullable=false, length=50)
private String name;

@Column(nullable=false)
private float shipping;

@Column(nullable=false)
private float price;
.............................
}

I tried something like this:


//remove bundle from configuration
config.getProductBundles().remove(bundle);
bundle.getConfigurations().remove(config);

//persist
saveBundle(bundle);
saveConfiguration(config);

The save* methods just call em.persist() but nothing happens. When persist is called it passes right through and no queries are even generated, as if nothing has changed.

I'm bumbling around in the dark here obviously and I'm sure the answer is simple, could someone give me some ideas?

Thanks!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mvatkina
Offline
Joined: 2005-04-04

I probably need more information about your actual test case. But here are some ideas:
You definitely do not need to call persist - they are managed instances already, right?.
If those instances are not managed by this EM, you need to call merge before removing relationship.
And don't forget to commit the transaction :).

HTH,
-marina

zambizzi
Offline
Joined: 2005-08-19

That's really all there is to it, honestly. A single form that allows you to choose which Configuration you'd like to remove from the ProductBundle (see my previous post w/ entities.)

The entities are managed but are disconnected...the method persists the entities checks for a PK and calls merge() if one exists, persist() not (new entity). In this case they are [i]always[/i] being merged, not persisted, since the Configuration entity will always be an existing record.

I've got @OneToOne and @OneToMany relationships all over my domain and persist/merge them just fine - it's only the @ManyToMany that I'm having a hard time understanding.

So, all that being said, what *should* I be doing since persist/merge doesn't do anything in my case.

mvatkina
Offline
Joined: 2005-04-04

If you still see this problem, please file a bug with the test case that reproduces the problem.

thanks,
-marina