I'm relatively new to Spring and JPA etc. I'm trying to create a many-to-many relationship between Tags and Customers (bi-directional)
I think I've got the relationship right. It all works well, except one problem. The Tag value is "Null" after it's saved to the database.
So what I'm doing is - adding a new List of Tags to the customer, then using the Cascade option to persist it when the customer is saved. I set a debug point before I call CustomerRepository.save(customer), and the tags all have values. After the save operation (on the customer repository) I observe two things - the Tags against the customer still aren't associated with an id, but in the database the tags are created but the values aren't stored in the column. So if I have 4 tags set against the customer, there will be 4 new rows in both the relationship table and the tag table, but the values will be null even though they're set correctly against the Tag object.
Also, if I call save against the tag repository directly before trying to save via the cascade, it saves the values and it all works (except for the transactionality aspects of it)...
Can anyone explain why the value column would be null when saving via the Cascade.persist option?
From Customer Class
@ManyToMany
@JoinTable(
name="customer_tags",
joinColumns={@JoinColumn(name="customerId", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="tagId", referencedColumnName="id")})
@Cascade(value = CascadeType.PERSIST)
private List<Tag> tags = new LinkedList<Tag>();
From Tag Class:
@ManyToMany(mappedBy = "tags")
private List<Customer> customers;
And my repository is this:
/**
* The tag interface is used to retrieve, save and manage customer tags.
*/
public interface TagRepository extends JpaRepository<Tag, Long> {
/**
* Finds a list of tags
* @return
*/
public List<Tag> findByTag(String tag);
}