Hibernate使用不可变列表初始化集合

huangapple go评论63阅读模式
英文:

Hibernate initializes collection with immutable list

问题

我有以下实体:

@Entity(name = "courses")
public class Course {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Singular
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private Collection<Comment> comments = new ArrayList<>();

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	private User createdBy;

	public void addComment(Comment comment) {
		comments.add(comment);
	}
}

当我从数据库加载课程时,评论会被初始化为持久集合(Persistent Bag)。问题出现在当我调用 addComment(comment) 方法时,我会得到以下异常:

Caused by: java.lang.UnsupportedOperationException: null
	at java.base/java.util.AbstractList.add(AbstractList.java:153) ~[na:na]
	at java.base/java.util.AbstractList.add(AbstractList.java:111) ~[na:na]
	at org.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:408) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]

看起来 Hibernate 分配的 Persistent Bag 是一个不可变的 Collection.EmptyList,因此无法添加评论。

我需要做哪些更改才能够添加评论呢?

英文:

I have the following entity:

@Entity(name = &quot;courses&quot;)
public class Course {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Singular
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private Collection&lt;Comment&gt; comments = new ArrayList&lt;&gt;();

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	private User createdBy;

	public void addComment(Comment comment) {
		comments.add(comment);
	}
}

When I load a course from the database, comments are initialized with a Persistent Bag. The problem occurs when I call addComment(comment), I then get this exception:

Caused by: java.lang.UnsupportedOperationException: null
	at java.base/java.util.AbstractList.add(AbstractList.java:153) ~[na:na]
	at java.base/java.util.AbstractList.add(AbstractList.java:111) ~[na:na]
	at org.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:408) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]

It seems that the Persistant Bag, which hibernates assigns, is a Collection.EmptyList, which is immutable and, as such, no comments can be added.

What do I need to change to be able to add comments?

答案1

得分: 3

好的,以下是您提供的内容的翻译:

好的,看起来 lombok 和 hibernate 并没有像我预期的那样很好地配合起来。此外,我将我的实体用以下 lombok 注解进行了注释:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Course {

    //...
 	
    @Singular
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private List<Comment> comments = new ArrayList<>();
}

当我从数据库加载一个实体时,似乎 lombok 使用了构建器模式来创建一个新的实例。这将导致评论(comments)被分配一个空的 Collection 列表,然后由于它是不可变的,就无法添加新的评论。我的解决方法是移除 @Singular 注解,并添加 @Builder.Default 注解。现在会分配一个 new ArrayList<> 而不是不可变的列表。虽然现在无法使用创建的单数构建器方法,但至少可以工作了。

英文:

Ok so it seems like lombok and hibernate where not really working together as I expected. In addition I had my entity annotated with the following lombok notations:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Course {

    //...
 	
    @Singular
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private List&lt;Comment&gt; comments = new ArrayList&lt;&gt;();
}

When I loaded an entity from the DB it seems lombok uses the builder pattern to create a new instance. This will result in comment having a Collection.EmptyList assigned and then no new comments can be added since it is immutable. My fix was/is to remove the @Singular and add a @Builder.Default. Now a new ArrayList&lt;&gt;() is assigned instead the immutable list. Now I can't use the created singular builder methods but at least it works.

huangapple
  • 本文由 发表于 2020年8月30日 02:26:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63650443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定