spring-projects / spring-projects/spring-data-jpa

Class Cast Exception While saving Nested entity [DATAJPA-1496]

Open
#1,809 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 30, 2020.

in: core type: bug
Dominant language
Java
Stars
3.3k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Description

Tarun Kumar Das opened DATAJPA-1496 and commented

Below Two are my entity 'View' and 'Button'

@Entity
@Table`(name = "[View]", catalog = "[CatalogName1]")
public class View {

private UUID id;

private String name;

private String title;

private List\<Button> buttons;

`@Id`
`@GeneratedValue`
`@Column`(name = "Id", unique = true, nullable = false, length = 36)
public UUID getId() {
return id;
}

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

`@Column`(name = "Name")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

`@Column`(name = "Title")
public String getTitle() {
return title;
}

`@OneToMany`(cascade = \{CascadeType.PERSIST, CascadeType.REMOVE})
`@JoinColumn`(name = "ViewId", referencedColumnName = "Id")
public List\<Button> getButtons() {
return buttons != null ? (List\<Button>) new ArrayList<>(buttons)
: null;
}

public void setButtons(List\<Button> buttons) {
this.buttons = (List\<Button>) buttons != null ? new ArrayList<>(buttons)
: null;
}
}

`@Entity`
`@Table`(name = "[Button]", catalog = "[CatalogName1]")
public class Button {

private UUID id;

private String text;

`@Id`
`@GeneratedValue`
`@Column`(name = "Id", unique = true, nullable = false, length = 36)
public UUID getId() {
return id;
}

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

`@Column`(name = "Text")
public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
}

 

I am using JPA repository to do CRUD operations.

We have sonar configured, which is suggesting to store or return a copy of object for collection type so i modified the getter and setter method like the above.

while updating some field of 'View', and then saving i am getting error for class cast exception

Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.hibernate.collection.spi.PersistentCollectionCaused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.hibernate.collection.spi.PersistentCollection at org.hibernate.event.internal.FlushVisitor.processCollection(FlushVisitor.java:39) at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:104)

 

 

Is there any way to resolve this. I can't directly return or store the value in getter and setter of "View" because of sonar issue


No further details from DATAJPA-1496

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.