Skip to content

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

Open
@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: coreIssues in core supporttype: bugA general bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions