I have scala+java project where some POJO classes written on Java, and controllers written on Scala. So i have an entity:
@Entity @Table
public class SomeEntity extends GenericEntity {
//... some fields
@OneToMany(...)
private List<SomeOtherEntity> someOtherEntities;
// getters, setters
}
and the following scala code:
val otherEntity = // construct object of type SomeOtherEntity
val entity = //got entity from database
entity.getSomeOtherEntities.add(otherEntity)
Last line produces java.lang.UnsupportedOperationException
at java.util.AbstractList.add
. I used some debugger magic and discovered that entity.getSomeOtherEntities
has type Wrappers$SeqWrapper
. Is there simple scala way to add an item to java list in scala? Ofc I can construct new java.util.List and add all items to it and then use field setter in entity class, but is there a simple way?
Finally I solved this. The problem was that I used scala.collection.JavaConverters
and did .asJava
on Scala List
somewhere. My application doesn't need to be particularly speedy, so I managed to solve it by replacing setter for SomeEntity.someOtherEntities
field with this one:
public void setSomeOtherEntities(List<SomeOtherEntity> someOtherEntities) {
this.someOtherEntities = new ArrayList<>();
this.someOtherEntities.addAll(someOtherEntities);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.