0

I've an User entity and Product entity with @ManyToMany relationship. I'm trying to store the user selected products into database.

User Entity:

@Entity
@Table(name = "user")
@Getter
@Setter
@ToString
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String username;
    private String password;

    @Transient
    private String passwordConfirm;

    @ManyToMany
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;

    @ManyToMany(mappedBy = "users")
    private Set<Product> products;    

}

Product Entity:

@Entity
@Table(name = "product")
@Getter
@Setter
@ToString
public class Product{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    private String category;
    private String name;

    @ManyToMany
    @JoinTable(name = "user_product", joinColumns = @JoinColumn(name = "product_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
    private Set<User> users;

}

Service:

@Service
public class ProductServiceImpl implements ProductService{

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public void save(Product product) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentPrincipalName = authentication.getName();
        User user = userRepository.findByUsername(currentPrincipalName);            
        Set<User> userSet = new HashSet<User>(); 
        userSet.add(user);
        product.setUsers(userSet);                      
        productRepository.save(product);
    }
}

Controller:

@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping(value = "/product/add", consumes = MediaType.APPLICATION_JSON_VALUE)
    public void add(@RequestBody Product product) {
        productService.save(product);       
    }
}

The join table user_product doesn't allow me to save same product_id with multiple user. It's keep deleting the already existing product_id (for previously saved user_id) and inserting it back with new user_id as shown below.

Hibernate: select users0_.product_id as product_id1_5_0_, users0_.user_id as user_id2_5_0_, user1_.id as id1_4_1_, user1_.password as password2_4_1_, user1_.username as username3_4_1_ from user_product users0_ inner join user user1_ on users0_.user_id=user1_.id where users0_.product_id=?       
Hibernate: delete from user_product where product_id=? and user_id=?
Hibernate: insert into user_product (product_id, user_id) values (?, ?)

How can I achieve by having join table allow combinations of product_id and user_id?

I'm using Spring Data JPA with H2 database and I made sure the relationships in the table columns are correct. If I manually insert record into the tables, it is allowing but not through the code. Can you correct me if I'm wrong in anywhere?

Nagu
  • 1
  • 1
  • 1
    Add your controller and service code also so that we can reproduce your problem – Eklavya Apr 14 '20 at 08:54
  • Can't tell for sure without the details asked for by Abinash Ghosh, but it might be this problem: https://stackoverflow.com/questions/40977239/jpa-onetomany-foreign-key-is-null – Jens Schauder Apr 14 '20 at 09:19
  • Thank you and sorry. I've edited the post with controller and service. – Nagu Apr 14 '20 at 14:24
  • Have you had a chance to look at my code? Thanks in advance! – Nagu Apr 17 '20 at 04:46

0 Answers0