我有一个实体类A,其子类为pendingUsers,如下所示。
A.java
$scope.removeDirCtrl = function (removeVal) { // function to be called from directive
console.log("ctrlRemove");
console.log(removeVal); //undefined
}
User.java
@Entity
public class A implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "PENDING", joinColumns = {@JoinColumn(name = "aId")}, inverseJoinColumns = {@JoinColumn(name = "userId")})
@Cascade(value = org.hibernate.annotations.CascadeType.ALL)
private Set<User> approvers = new HashSet<User>();
@Column(name = "active", nullable = false, columnDefinition = "varchar default 'Y'")
@org.hibernate.annotations.Type(type = "yes_no")
private boolean active = true;
@NotBlank
@Column(nullable = false, unique = true)
private String poNo;
}
我想检索已将userId作为批准者之一的所有A对象。如何用hibernate Criteria写这个?
当我进行一对一的映射时,我写的如下。
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@NotBlank(message = "User ID cannot be empty")
@Column(unique = true, nullable = false, updatable = false)
private String userId;
}
正如我们现在改变多对多的映射如何实现这一目标?
答案 0 :(得分:2)
我对此很陌生,但是我已经在我正在处理的项目中成功地使用了这个方法。
我已经使用了与上面提到的代码类似的代码。但是,如果不起作用,您可以使用Join进行操作,如下所示:
createEntityCriteria()
.createAlias("approvers", "apprs", JoinType.INNER_JOIN)
.add(Restrictions.eq("apprs.userId", userId))
.add(Restrictions.eq("active", true)).list();
INNER_JOIN将仅返回具有批准者的记录,LEFT_OUTER_JOIN将返回所有可用批准者的父行。
使用:org.hibernate.sql.JoinType
希望这有帮助。