NotSerializableException in a ManagedBean with ViewScoped and Spring's services - serialization

This is the ManagedBean
#ManagedBean #ViewScoped public class DetailItem {
private static final long serialVersionUID = -7647929779133437125L;
#ManagedProperty(value = "#{itemServiceImpl}")
private ItemService servItem;
This is the Service
#Service("itemServiceImpl") #Transactional(value = "transactionManagerLocal") public class ItemServiceImpl implements ItemService {
private static final long serialVersionUID = 1L;
#Autowired
#Qualifier("itemDaoImpl")
private ItemDAO dao;
but when I try to access to the page that used 'DetailItem', I have the following exception:
java.io.NotSerializableException: org.springframework.dao.support.PersistenceExceptionTranslationInterceptor java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164) java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
To fix that I do the servItem transient and obtain it from the applicationContext. But I understand that it is not the correct solution and I dont find any other. Which is the proper way to do that?
I donĀ“t have the exception with sessionscoped or requestscoped.

Sounds like a similar problem like Serialization of ManagedProperty
Does ItemService implement Serializable and are all members of ItemServiceImpl serializable themselves?

Related

How to atomically update a map key in Aerospike?

#AerospikeEntity(nameSpace = Constants.NAMESPACE, setName = Constants.ABC_SET)
public class ABCModel implements Serializable {
private static final long serialVersionUID = 1L;
#AerospikeKey
private String cycle;
private long debitAmount;
private long creditAmount;
private Map<String, AtomicDouble> map;
}
suppose atomically i want to update some key inside this given map ,how can i achieve this in Aerospike java client ?
Please see code examples here: https://github.com/aerospike/aerospike-client-java/blob/master/examples/src/com/aerospike/examples/OperateMap.java

JMX and different class loaders

My project is multi-module and is structured in this way:
common (JMX MBean interfaces).
agent (JMX MBean implementations and instrumentation code - here I register the MBeans)
plugin (here I "ask" for registered MBeans)
The problem is the plugin part uses a different classloader than the agent. Thus I receive
java.lang.ClassCastException: class com.github.lppedd.jmx.FeaturesMBean$Feature cannot be cast to class com.github.lppedd.jmx.FeaturesMBean$Feature
(com.github.lppedd.jmx.FeaturesMBean$Feature is in unnamed module of loader 'app';
com.github.lppedd.jmx.FeaturesMBean$Feature is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader #35962a94)
The incriminated MBean is
public interface FeaturesMBean {
List<Feature> availableFeatures();
class Feature implements Serializable {
private static final long serialVersionUID = 1L;
public final String name;
public final String description;
public Feature(final String name, final String description) {
this.name = name;
this.description = description;
}
}
}
How should I deal with this issue?

variable not taking in consideration with velocity

I want to generate java class.
I create a template with velocity
#Repository
public class $className extends BaseRepository<$bean, $searchDto> {
#PersistenceContext
private EntityManager em;
#Autowired
public $className(EntityManager em) {
super($bean.class, em);
}
}
If I pass this value
className=UsersRepositoryImpl
bean=Users
searchDto=UsersSearch
I get
#Repository
public class UsersRepositoryImpl extends BaseRepository<Users, UsersSearch> {
#PersistenceContext
private EntityManager em;
#Autowired
public UsersRepositoryImpl(EntityManager em) {
super(class java.lang.String, em);
}
}
seem like a issue with $bean.class
tried "$bean.class" but get same issue
Edit
put {} around work
${bean}
You need to call
${bean}.class
which tells the velocity processor that ${bean} is the velocity variable and .class is just static text of the template. Othherwise he will interpret $bean.class as one expression, your $bean is a String and $bean.class is therefore printed using the toString method of the String's Class object.

Entry and Serialisation

My project is in the form:
Class Persistant :
#Entity
public class Produit implements Serializable {
private static final long serialVersionUID = -3352484919001942398L;
#Id
#GeneratedValue
private Long id;
private String module;
private String produit;
//getter&&setter
Class Dao
public List<Entry<Integer, List<Produit>>> parProduit(String cat) {
.......
HashMap<Integer, List<Produit>> legaux = new HashMap<Integer, List<Produit>>();
........
List<Map.Entry<Integer, List<Produit>>> entries = new ArrayList<Entry<Integer, List<Produit>>>(legaux.entrySet());
return entries;
}
when i execute this code i get this error :
java.io.NotSerializableException: java.util.HashMap$Node
java.util.HashMap.EntrySet<K, V>
is not serializable.
legaux.entrySet()
probably returns set of type java.util.HashMap.EntrySet, you may want to check that.

bind to property always return null

I am trying to bind a repository to property using Ninject but always get null reference of binding object. I will explain the problem using code below.
public interface IServiceRepository
{
User GetUser(string email);
IQueryable<Statistic> GetStatisticForCurrentMonth(string ip);
void InsertStatistic(ConversionModel conversionModel);
class ServiceRepository : IServiceRepository
{
//Implementation of the Interface
}
I am would like to bind the repository above to class below while the class is created. Unfortunately Repository object is always null. Maybe I have misunderstood how Ninject is working? How to solve the problem?
public class Converter
{
[Inject]
public static IServiceRepository Repository { get; set; }
private static Converter _converter;
public static Converter Instance
{
get { return _Converter ?? (_Converter = new Converter ());
}
}
Ninject activator code
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IServiceRepository>().ToMethod(context => Converter.Repository);
}
Update
I have tried to rewrite code like this
public class Converter
{
private readonly IServiceRepository _repository;
public Converter(IServiceRepository repository)
{
_repository = repository;
}
//skip code
}
The test...
[TestMethod]
public void ConverterInstanceCreated()
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<IServiceRepository>().To<ServiceRepository>();
Assert.IsNotNull(kernel.Get<Converter>());
}
}
gives exception
Test method PC.Tests.NinjectTest.ConverterInstanceCreated threw exception:
Ninject.ActivationException: Error activating IServiceRepository
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IServiceRepository into parameter repository of constructor of type Converter
1) Request for Converter
I just lost, I am trying to understand how Ninject is working for about week without any success. In my case why this exception is thrown?
Also please someone post working example with one repository injection to singleton class.
Ninject does not inject statics. Change the coynverter to a non-static class and configure it as Singleton in ninject. Also use constructor injection and make the repo a private field.
Now you can inject the converter to the constructors where you need it.
Even though you are using Property injection and not Constructor injection I think it would still be
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IServiceRepository>().To<ServiceRepository>();
}
As ninject still just needs to know what concrete type to map to the Interface
I haven't tested this so apologies if it's wrong.