当我使用@OrderColumn注解时,Hibernate会返回包含42075条[空]记录的集合,但如果没有@OrderColumn,为什么一切都能正常工作?我想使用字段“OrderNumber”来始终按该字段对实体进行排序。在PostgreSQL端,这个OrderNumber的类型是“Serial”,自动递增计数。
DocTestEntity:
@Entity
@Table(name = "`Document`")
public class DocTestEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "`Document_ID`")
private Integer id;
@ManyToMany
@JoinTable(name = "`DocumentEmployee`",
joinColumns = @JoinColumn(name = "`Document_ID`"),
inverseJoinColumns = @JoinColumn(name = "`Employee_ID`"))
@OrderColumn(name ="`OrderNumber`", updatable = false, insertable = false)
private List<EmployeeTestEntity> employeeEntityList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<EmployeeTestEntity> getEmployeeEntityList() {
return employeeEntityList;
}
public void setEmployeeEntityList(List<EmployeeTestEntity> employeeEntityList) {
this.employeeEntityList = employeeEntityList;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DocTestEntity docEntity = (DocTestEntity) o;
return Objects.equals(id, docEntity.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "DocTestEntity{" +
"id=" + id +
'}';
}
}
EmployeeTestEntity:
@Entity
@Table(name = "`Employee`")
public class EmployeeTestEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "`Employee_ID`")
private Integer id;
@Column(name = "`Employee_name`")
private String name;
@Column(name = "`Employee_surname`")
private String surname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String employeeName) {
this.name = employeeName;
}
public String getSurname() {
return surname;
}
public void setSurname(String employeeSurname) {
this.surname = employeeSurname;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EmployeeTestEntity that = (EmployeeTestEntity) o;
return Objects.equals(id, that.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "EmployeeTestEntity{" +
"id=" + id +
'}';
}
}
和测试DAO:
@Stateless
public class DocTestDAO {
@PersistenceContext
private EntityManager em;
public DocTestEntity selectDocumentByID(Integer id) {
var result = em.createQuery("SELECT DISTINCT a from DocTestEntity a " +
" WHERE a.id = :id ", DocTestEntity.class)
.setParameter("id", id)
.getResultStream()
.map(Optional::ofNullable)
.findFirst()
.flatMap(Function.identity())
.orElse(null);
System.out.println("List records count is: " + result.getEmployeeEntityList().size());
return result;
}
}
1条答案
按热度按时间knpiaxh11#
您不需要自动递增
OrderNumber
列,Hibernate可以根据集合employeeEntityList
中项的顺序来管理Order列本身。您应该将
OrderNumber
设置为可插入和可更新(默认情况下为真):我建议清理代码,从列名
"
DocumentEmployee"
中删除这些撇号应该是"DocumentEmployee"
。您提到没有@OrderColumn
任何东西都可以工作,所以我认为撇号不会影响功能,但看起来很奇怪。请让我知道,如果这仍然不工作后提到的更新。