Java 如何在Hibernate搜索中正确使用@ContainedIn注释?
我正在使用Hibernate和PostgreSQL 和Hibernate搜索(Java 如何在Hibernate搜索中正确使用@ContainedIn注释?,java,
elasticsearch,hibernate-search,Java,
elasticsearch,Hibernate Search,我正在使用Hibernate和PostgreSQL 和Hibernate搜索(5.7.0.Alpha1) 使用ElasticSearch(2.4.2) 我有两门课:Book和Author。 一本书可以有许多作者 一个作者可以写不止一本书 我对这本书的注释如下: import java.util.ArrayList; import java.util.List; import javax.persistence.*; import org.hibernate.search.annotations.
5.7.0.Alpha1
)
使用ElasticSearch(2.4.2
)
我有两门课:Book
和Author
。
一本书可以有许多作者
一个作者可以写不止一本书
我对这本书的注释如下:
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import org.hibernate.search.annotations.*;
@Indexed
@Entity
public class Book implements Record {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
protected Long id;
@Field
protected String title;
@ManyToMany
@JoinTable(name = "books_authors",
joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id"))
@IndexedEmbedded(includeEmbeddedObjectId = true)
protected List<Author> authors = new ArrayList<>();
//constructors, getters and setters
//...
}
ElasticSearch中的作者文档将得到更新,
但图书文件不会改变:
{"_index":"com.example.app.model.author","_type":"com.example.app.model.Author","_id":"2","_score":1,
"_source":{"name":"Author 02"}}
{"_index":"com.example.app.model.book","_type":"com.example.app.model.Book","_id":"3","_score":1,
"_source":{"title":"Elementy","authors":[{"name":"Author 01", "id":"2"}]}}}
我读到,对于这种情况,我需要在作者
侧使用@ContainedIn
注释,
所以我做了。由于这个原因,我需要添加属性authoredBooks
,之前我没有计划
package com.example.app.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import org.hibernate.search.annotations.*;
@Indexed
@Entity
public class Author {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
protected long id;
@Field
protected String name;
@ContainedIn
@ManyToMany(mappedBy = "authors")
private List<Book> authoredBooks = new ArrayList<>();
//constructors, getters and setters
//...
}
package com.example.app.model;
导入java.util.ArrayList;
导入java.util.List;
导入javax.persistence.*;
导入org.hibernate.search.annotations.*;
@索引
@实体
公共类作者{
@身份证
@GeneratedValue(策略=GenerationType.SEQUENCE)
保护长id;
@场
受保护的字符串名称;
@包含
@许多(mappedBy=“作者”)
private List authoredBooks=new ArrayList();
//构造函数、getter和setter
//...
}
然而,这并没有改变Hibernate搜索的行为,
所以图书文档仍然没有更新。
你能给我一些提示我能做什么或检查什么吗
我将Hibernate与PostgreSQL结合使用,将Hibernate搜索(5.7.0.Alpha1)与ElasticSearch(2.4.2)结合使用
首先要做的是升级到HibernateSearch的实际版本(不是Alpha或Beta版),以确保您没有遇到自那以后已经解决的错误
除此之外。。。您的初始代码只更新关系的一方;您是否确保在ook01.addAuthor(author01)
之后添加一行author01.addBook(book01)
?非常感谢您的帮助,它解决了我的问题。我没有发布author01.addBook(book01)
而是从数据库中强制更新author01
对象(我还添加了cascade
和fetch
注释),因为我认为这是一种更现实的设置。更新author01
后,将book01
包含在其列表中,然后@ContainedIn
工作-图书文档在ElasticSearch中得到更新。谢谢@nuoritoveri-在你的问题中,你写道“出于这个原因,我需要添加authoredBooks属性”。Yoann Rodière的修复是作为一个独立的解决方案工作的,还是您提到的属性加上ContainedIn注释也是解决方案的一个关键部分?我必须用@ContainedIn
添加authoredBooks
,然后触发对象的更新,因此这两个元素都是必需的。
package com.example.app.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import org.hibernate.search.annotations.*;
@Indexed
@Entity
public class Author {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
protected long id;
@Field
protected String name;
@ContainedIn
@ManyToMany(mappedBy = "authors")
private List<Book> authoredBooks = new ArrayList<>();
//constructors, getters and setters
//...
}