[英]JPA OneToMany , how to ignore field ManyToOne when print in console or webapp
我有2个表格:类型(genre_id,genre_name)和电影(movie_id,movie_name,movie_score,genre_id)。 风格中引用genre_id的电影中的genre_id_fk。
@Entity
@Table(name = "genres", schema = "test")
public class Genre {
@Id
@Column(name = "genre_id")
private int id;
@Column(name = "genre_name")
private String name;
@OneToMany(mappedBy = "genre", fetch = FetchType.LAZY)
private List<Movie> movies = new ArrayList<>();
}
电影的第二实体
@Entity
@Table(name = "movies", schema = "test")
public class Movie {
@Id
@GeneratedValue
@Column(name = "movie_id")
private int id;
@Column(name = "movie_name")
private String name;
@Column(name = "movie_score")
private double score;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "genre_id")
private Genre genre;
}
当我尝试使用此代码在控制台中打印时:
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
EntityManager em = emf.createEntityManager();
Genre genre = em.find(Genre.class, 1);
System.out.println(genre);
}
仅在电影类字段“类型”中的toString()中删除,才能Exception in thread "main" java.lang.StackOverflowError
接收到Exception in thread "main" java.lang.StackOverflowError
可以解决该问题。 但是有可能避免吗? 和Spring Boot应用程序相同的问题
@RestController
public class GenreController {
@Autowired
private GenreService genreService;
@RequestMapping("/test/{id}")
public List<Genre> getGenreInfo(@PathVariable int id){
return genreService.getGenreFilms(id);
}
}
这里服务
@Service
public class GenreService {
public List<Genre> getGenreFilms(int id){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
EntityManager em = emf.createEntityManager();
List<Genre> genres = new ArrayList<>();
Genre genre = em.find(Genre.class, id);
genres.add(genre);
return genres;
}
}
并收到以下问题:[{“ id”:1,“ name”:“ Thriller”,“ movies”:[{“ id”:1,“ name”:“好吗?”,“得分” :5.45,“类型”:{“ id”:1,“名称”:“惊悚片”,“电影”:[{“ id”:1,“名称”:“任何,你还好吗?”,“得分” :5.45,“类型”:{“ id”:1,“名称”:“惊悚片”,“电影”:[{“ id”:1,“名称”:“任何,你还好吗?”,“得分” :5.45,“ genre”:....并无穷无尽。 控制台我可以通过忽略toString()方法中的字段来解决。 但是如何在Web应用程序中解决此问题?
在控制台打印时,这里是休眠调试日志
22:15:37.154 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad -
Resolving associations for [com.company.Genre#1]
22:15:37.164 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Done
materializing entity [com.company.Genre#1]
22:15:37.164 [main] DEBUG
org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl -
HHH000387: ResultSet's statement was not registered
22:15:37.165 [main] DEBUG
org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader - Done
entity load : com.company.Genre#1
22:15:37.165 [main] DEBUG
org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl -
Initiating JDBC connection release from afterTransaction
22:15:37.167 [main] DEBUG org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer
- Loading collection: [com.company.Genre.movies#1]
22:15:37.167 [main] DEBUG org.hibernate.SQL - select movies0_.genre_id as
genre_id4_1_0_, movies0_.movie_id as movie_id1_1_0_, movies0_.movie_id as
movie_id1_1_1_, movies0_.genre_id as genre_id4_1_1_, movies0_.movie_name as
movie_na2_1_1_, movies0_.movie_score as movie_sc3_1_1_ from test.movies
movies0_ where movies0_.genre_id=?
Hibernate: select movies0_.genre_id as genre_id4_1_0_, movies0_.movie_id as
movie_id1_1_0_, movies0_.movie_id as movie_id1_1_1_, movies0_.genre_id as
genre_id4_1_1_, movies0_.movie_name as movie_na2_1_1_, movies0_.movie_score
as movie_sc3_1_1_ from test.movies movies0_ where movies0_.genre_id=?
22:15:37.168 [main] DEBUG
org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl -
Preparing collection intializer : [com.company.Genre.movies#1]
22:15:37.170 [main] DEBUG
org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl -
Starting ResultSet row #0
22:15:37.171 [main] DEBUG org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerIm
pl - Found row of collection: [com.company.Genre.movies#1]
22:15:37.171 [main] DEBUG
org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl -
Starting ResultSet row #1
22:15:37.172 [main] DEBUG org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerIm
pl - Found row of collection: [com.company.Genre.movies#1]
22:15:37.172 [main] DEBUG
org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl -
Starting ResultSet row #2
22:15:37.172 [main] DEBUG org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerIm
pl - Found row of collection: [com.company.Genre.movies#1]
22:15:37.172 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad -
Resolving associations for [com.company.Movie#1]
22:15:37.172 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Done
materializing entity [com.company.Movie#1]
22:15:37.173 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad -
Resolving associations for [com.company.Movie#2]
22:15:37.173 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Done
materializing entity [com.company.Movie#2]
22:15:37.173 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad -
Resolving associations for [com.company.Movie#3]
22:15:37.173 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Done
materializing entity [com.company.Movie#3]
22:15:37.173 [main] DEBUG
org.hibernate.engine.loading.internal.CollectionLoadContext - 1 collections
were found in result set for role: com.company.Genre.movies
22:15:37.173 [main] DEBUG
org.hibernate.engine.loading.internal.CollectionLoadContext - Collection
fully initialized: [com.company.Genre.movies#1]
22:15:37.173 [main] DEBUG
org.hibernate.engine.loading.internal.CollectionLoadContext - 1 collections
initialized for role: com.company.Genre.movies
22:15:37.173 [main] DEBUG
org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl -
HHH000387: ResultSet's statement was not registered
22:15:37.173 [main] DEBUG org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer
- Done loading collection
Exception in thread "main" java.lang.StackOverflowError
告诉我我在哪里做错了,以及如何解决或谷歌这个问题? 只是不通过localhost:8080 / genre / id打印此对象? 做一些特别的印刷品或什么?
好像你想的时候有一个无限循环toString
您的Genre
实体。 您的代码首先通过id加载您的Genre实体,然后调用Genre.toString()
。 因为您与Movie
具有@OneToMany
关系,所以它会lazy loads
列表,然后为与该流派相关的每个电影调用Movie.toString()
。 然后,对于每部电影,您都与Genre有@ManyToOne
关系。 问题就在这里。 它将为列表中的每个电影再次调用Genre.toString()
。
Genre.toString()
包括电影列表。 @JsonBackReference
添加到Movie
的@ManyToOne
关系中,这样,当Jackson映射到此处的 Json Annotation文档时,Jackson将忽略它 Movie
属性。 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.