user2462794
user2462794

Reputation: 305

JoinColumn on column created from another join

I have entity User which contains field with entity Actor which contains field with entity Product. Now I want User in database to have information about product id that's being stored in Actor using @JoinColum on Product. For better clarification, some code:

   @Entity
    public class User {
        @ManyToOne
        @JoinColumns(value = {@JoinColumn(name = "actor_id", referencedColumnName = "id"),
                @JoinColumn(name = "product_id", referencedColumnName = "product_id")})
        private Actor actor; 

  public Actor getActor() {
        return actor;
    }

    public void setActor(Actor actor) {
        this.actor = actor;
    }
    }

    @Entity
    public class Actor {
        @ManyToOne
        @JoinColumn(name = "product_id")
        private Product product;
    }

In database it creates table "users" with "actor_id" and "product_id" and table "actors" with column "product_id".

Using Spring Data Repository's userRepository.findAll() method I end up with ClassCastException and the following message:

Actor_$$_jvst485_0 cannot be cast to java.io.Serializable

Any ideas how to fix it?

Upvotes: 1

Views: 254

Answers (1)

Aleksandr  Primak
Aleksandr Primak

Reputation: 111

You need to implement Serializable interface in all entities if you are passing detached object of them. This is specified by JSR 220. For more details, you can see this answer

Upvotes: 1

Related Questions