I am trying to create an application with 2 microservices. I have a spring boot app with two modules: device and user. Now I need to make the following mapping: one user can have multiple devices and a device can belong to one or more users, so many to many relationship. I got stuck because User is in one module and Device is in other module. To create the relationship I need to add cycle dependency between User and Device modules.
This is my User class from User module
package com.demo.user.entity;
import com.demo.device.entity.Device;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@Entity
@Table(name = "user")
@AllArgsConstructor
@NoArgsConstructor
public class User implements UserDetails{
@Id
private int id;
private String name;
private String role;
private String password;
public User(@Lazy Set<Device> devices){
this.devices = devices;
}
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "user_device",
joinColumns = {
@JoinColumn(name = "user_id",referencedColumnName = "id")
},
inverseJoinColumns = {
@JoinColumn(name = "device_id",referencedColumnName = "id")
})
private Set<Device> devices;
public User(String name, String role, String password) {
this.name = name;
this.role = role;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority(role));
}
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return name;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
public void setPassword(String password) {
this.password = password;
}
}
And this is my Device class from Device module
package com.demo.device.entity;
import com.demo.user.entity.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import java.util.Set;
@Entity
@Table(name = "device")
@AllArgsConstructor
@NoArgsConstructor
public class Device{
@Id
private int id;
private String description;
private String address;
private int consumption;
public Device(@Lazy Set<User> users){
this.users = users;
}
@ManyToMany(mappedBy = "device", fetch = FetchType.LAZY)
private Set<User> users;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getConsumption() {
return consumption;
}
public void setConsumption(int consumption) {
this.consumption = consumption;
}
}
When I try to build the project, i get the following error: java: Annotation processing is not supported for module cycles. Please ensure that all modules from cycle [device,user] are excluded from annotation processing
I tried adding the @Lazy annotation but it's not working. I also tried to uncheck from properties "Enable annotation processing" but it keeps getting checked again on the next run...
Please help me!