我有实体组织,员工,角色
@Entity
public class Organization
{
....
@OneToMany
List<Employee> employees;
}
@Entity
public class Employee
{
....
@OneToMany
List<Role> roles;
}
@Entity
public class Role
{
....
String roleName;
}
我希望为所有与实体无关的获取结果(可以是组织,员工或角色)保持相同的结构,该实体将具有所需的实体字段,所有其他字段将为空。
让我们说我有角色列表。我可以通过以下代码实现我的要求
Employee employee = new Employee();
employee.setRoles(roles );
Organization org = new Organization();
org.setEmployee(employee);
。但是如果没有上面的setter和构造函数代码,有没有办法在下面的结构上实现?
{
...
employees:[
roles:[
{
...
},
{
....}
],
....
]
}
有没有办法做到这一点?
答案 0 :(得分:0)
如果我正确理解Employee
有roleId
。如果是这种情况,您可以使用Role role
在 Employee.class 中设置Long roleId
对象而不是@JoinColumn
:
@ManyToOne
@JoinColumn(name="roleId", referencedColumnName="id")
private Role role;
请注意,name="roleId"
是来自 Employee.class 的归档,而referencedColumnName="id"
是来自 Role.class <的id
字段/强>