Atul
Atul

Reputation: 1590

@ManyToMany Hibernate only update JoinTable

I have below entities and @ManyToMany mapping between the two.

@Entity
@Table(name = "user")
public class User implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 5340562707217344212L;  
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;
    private String userName;
    private String password;
    private String firstName;
    private String lastName;
    private String emailId;
    private Date createdDate;
    private Byte status;
    private Date lastModifiedDate;  
    @ManyToMany
    @JoinTable(name = "user_products_mapper",
               joinColumns = @JoinColumn(name = "user_id"),
               inverseJoinColumns = @JoinColumn(name = "product_id")
    )
    private List<Products> products = new ArrayList<Products>();

    public void addProducts(Products product){

        this.products.add(product);     
    }


@Entity
@Table(name="products")
public class Products implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1895580713896126954L;
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long productId;
    private String productName;
    private String description;
    private double minBalance;


    public Long getProductId() {
        return this.productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return this.productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

My question is :

1)I already have these entities persisted. (User and Products)

2)User and Products are already exists in databasae and relation between them is @ManyToMany

3)I want to insert records ONLY IN JoinTable (user_products_mapper) since the rows are already present for User and Products.

4)One way to achieve this is to use UNIDIRECTIONAL mapping as I have above and just call

User.addProducts

5) Before doing this User and Products entities will be fetched like below :

User user = this.userDao.findOne(userId);
        if(user == null){
            //throw exception 
        }
        Products product = this.productDao.findOne(productId);

6)Is there any way of doing this ?

Upvotes: 1

Views: 627

Answers (1)

Tobb
Tobb

Reputation: 12180

When using Hibernate/JPA you do not control the actual queries being run, but you are not supposed to either.

What you would do to connect a user and a product would be something like:

@Transactional
public void addProductToUser(final Long userId, final Long productId) {
    final User user = em.find(User.class, userId);
    final Product product = em.find(Product.class, productId);

    user.addProduct(product);
}

This will result in more queries than just inserting a row (since you will fetch both Product and User), but what you gain is simple and plain Java code. The cost of the extra queries in most cases is well within what is acceptable.

Upvotes: 3

Related Questions