gpt4 book ai didi

java - Hibernate如何按所有字段进行搜索

转载 作者:行者123 更新时间:2023-12-01 23:19:01 27 4
gpt4 key购买 nike

我想知道,如何在我的 dao 文件中编写该方法来获取与某个正则表达式匹配的所有对象?如何实现所有字段排除字段组的搜索?

例如,用户输入文本字段并获取符合条件{可以有一些符号}{userText}{可以有一些符号}的教师类对象列表。

如何绕过所有字段排除组

这是我的实体类

@Entity
@Table(name="teacher", schema = "public")//, schema = "public"
@SuppressWarnings("unused")
public class Teacher extends Human implements Serializable {

@Id
@Column(name="teacher_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

@ManyToMany(targetEntity = Group.class,fetch=FetchType.EAGER)
@JoinTable(name = "teacher_groups",
joinColumns = { @JoinColumn(name = "teacher_id") },
inverseJoinColumns = { @JoinColumn(name = "group_id") })
private List<Group> groups = new ArrayList();//<>

public List<Group> getGroups() {
return groups;
}
public void setGroups(ArrayList<Group> groups) {
this.groups = groups;
}

public void addGroup(Group group){

if (!(this.groups.contains(group))){
groups.add(group);
}
}

public void removeGroup(Group group){
for (int i = 0; i < groups.size(); i++) {
if (groups.get(i).getId().equals(group.getId()))
groups.remove(i);
}
}

public Teacher(String fam, String name, String otch, Date dateOfBirth, String phoneNumber, ArrayList<Group> groups) {
super(fam, name, otch, dateOfBirth, phoneNumber);
this.groups = groups;
}

public Teacher(String fam, String name, String otch, Date dateOfBirth, String phoneNumber) {
super(fam, name, otch, dateOfBirth, phoneNumber);
}

@Override
public String toString() {
return "Teacher{" + id + " "+
"groups=" + groups +
"} " + super.toString();
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Teacher(){}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Teacher that = (Teacher) o;
return this.getId().equals(that.getId());
}
}

这是我的 dao 类

@Repository
public class TeacherDaoImlp implements TeacherDao {

@PersistenceContext
private EntityManager em;

@Override
public void add(Teacher teacher){
em.persist(teacher);
}

@Override
public List<Teacher> getTeachersList(){
CriteriaQuery<Teacher> criteriaQuery = em.getCriteriaBuilder().createQuery(Teacher.class);
Root<Teacher> root = criteriaQuery.from(Teacher.class);
return em.createQuery(criteriaQuery).getResultList();
}

@Override
public void update(Teacher teacher) {
em.merge(teacher);
}

@Override
public Teacher findById(Long teacherId) {
Teacher teacher = em.find(Teacher.class,teacherId);
if (teacher==null)
throw new EntityNotFoundException("Teacher with ID = " + teacherId + " not found");
return teacher;
}

@Override
public void delete(Long teacherId) {
Teacher teacher = em.find(Teacher.class, teacherId);
if (teacher != null) em.remove(teacher);
else throw new EntityNotFoundException("Teacher with ID = " + teacherId + " not found");
}

}

最佳答案

您应该看看Hibernate Search 。它是 lucene 和 hibernate 的混合体。

索引字段映射将如下所示:

  @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
private String phoneNumber;

以及用于搜索的 dao 方法:

FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Teacher.class).get();
org.apache.lucene.search.Query luceneQuery = qb
.keyword()
.onFields("fam", "name", "otch", "dateOfBirth", "phoneNumber")
.matching("user text")
.createQuery();

Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Teacher.class);
List result = jpaQuery.getResultList();

依赖关系:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.11.3.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-elasticsearch</artifactId>
<version>5.11.3.Final</version>
</dependency>

关于java - Hibernate如何按所有字段进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58334970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com