首页 > 解决方案 > 如何在spring jpa中定义单向一对多dto

问题描述

我已经尝试了几天来解决这个问题,但我似乎无法弄清楚。我有 3 个表:用户、员工(扩展用户)和角色。我遇到的问题是我似乎无法找到一种方法让休眠了解我想要用户和角色之间的单向一对多。我不想从 Roles 表端获取所有用户。使用下面的配置,当我尝试在表用户中插入一个新值时,它不是使用 Roles 中的现有值,而是插入一个新值,从而产生具有不同 id 的重复值。你有什么建议吗?谢谢你。下面是我使用的类:

用户实体:

@Entity
@Table(name = "users")
@Inheritance(strategy = InheritanceType.JOINED) // used for the Employees table
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
private String e_mail;
private String phoneNumber;
private String name;
private String surname;
private Date birthDate;
private String adress;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "role_id", referencedColumnName = "id", nullable = false)
private Role role;

public User() { }

public User(String username, String password, String e_mail, String phoneNumber, String name,
            String surname, Date birthDate, String adress, Role role) {
    this.username = username;
    this.password = password;
    this.e_mail = e_mail;
    this.phoneNumber = phoneNumber;
    this.name = name;
    this.surname = surname;
    this.birthDate = birthDate;
    this.adress = adress;
    this.role = role;
}

//getters and setters
}

角色实体:

@Entity
@Table(name = "roles")
public class Role {

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

private String name;

public Role() { }

public Role(String name) {
    this.name = name;
}

//getters and setters
}

用户Dto:

public class UserDto {

@JsonIgnore
private Long id;
private String username;
private String password;
private String e_mail;
private String phoneNumber;
private String name;
private String surname;
private Date birthDate;
private String adress;
@JsonIgnore
private RoleDto role = new RoleDto();

public UserDto() { }

// getters and setters
}

角色Dto:

public class RoleDto {

@JsonIgnore
private Long id;
private String name;

public RoleDto() { }

// getters and setters
}

用户服务:

@Service
public class UserService {

private UserRepository userRepository;
private ModelMapper modelMapper = new ModelMapper();

public UserService(RoleService roleService, UserRepository userRepository, RoleRepository roleRepository) {
    this.roleService = roleService;
    this.userRepository = userRepository;
    this.roleRepository = roleRepository;
}

public void addUser(UserDto userDto) {
    userDto.setRole(roleService.getBuyerRoleDto());
    userRepository.save(modelMapper.map(userDto, User.class));
}

// other services...
}

角色服务:

@Service
public class RoleService {

private RoleRepository roleRepository;
private ModelMapper modelMapper = new ModelMapper();

public RoleService(RoleRepository roleRepository) {
    this.roleRepository = roleRepository;
}

/*
* i've tested the queries getById and getBuyerId and they are working as they should
*/
public RoleDto getBuyerRoleDto() {
    return modelMapper.map(roleRepository.getbyId(roleRepository.getBuyerId()), RoleDto.class);
}

// other services...
}

标签: javaspringhibernatejpamodelmapper

解决方案


您应该提供属性@ManyToOne(optional = false),以便这种关系成为强制性的。


推荐阅读