2

I have a problem with Spring Data, I receive some exception of NotSerializableException type.

Below an example to explain how I have this problem :

@Component
@Scope("session")
public class Bean implements Serializable {

   @Autowired
   private  FooRepository repository;

}

public interface FooRepository extends JpaRepository<Foo, Long>

After I look into in the code, I have seen that the interface org.springframework.data.repository.Reposotiry is not serializable.

And the last version of Spring Data hasn't changed.

I could override the serilization but I don't know if it's the best solution.

Could you tell me if you have found an other solution for this problem or workaroud.

Thank in advance.

alexbt
  • 16,415
  • 6
  • 78
  • 87
user2049200
  • 127
  • 2
  • 10

1 Answers1

1

You will "get the exception" when an instance must implement Serializable. The exception is thrown by either the serialization run-time or by the instance of the class.

I think the simplest fix is to make FooRepository "serializable", but eventually you cannot since it's a third-party library. So, in your case you must "mark" it as transient; once you do that it will be ignored by the serializable run-time.

@Component
@Scope("session")
public class Bean implements Serializable {

   @Autowired
   private transient FooRepository repository;

}

NOTE: That's in theory, I've never done that with an injected bean, but the result should be the same. Anyway, that applies to your issue, maybe not to your specific solution.

x80486
  • 6,627
  • 5
  • 52
  • 111