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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.