DynaBeans vs CodeGenerated JavaBeans performance implications - java

I have to use JavaBeans in my application.
The application is a config driven application. Depending on config, different JavaBeans Classes will be required.
One option is that depending on configuration, I use a code generator to generate JavaBean classes.
Other option that sounds very appealing is use Dynamic Beans from Apache Beanutils. It saves me from one extra step of Code generation.
Can you please help me that what are the performance and memory implications of using Dynabeans vs Generated JavaBeans.
Is there any better alternative to DynaBeans?
In both cases, I will be using Apache BeanUtils to invoke getters/setters later.

I have been looking at BeanUtils implementation of BasicDynaBean and have reached at following conclusion regarding comparison with Code Generated JavaBean.
Memory
BasicDynaBean uses a HashMap to store keys/values. If there are 1000 instances of sme DynaBean then lot of memory is being wasted because keys are being stored again in each instance. Therefore it is more memory consuming that a code generated JavaBean and would not recommend this if you are going to store a large no of instances of Dynabean in memory.
Speed
To access the different fields, it invokes get/put methods on HashMap. Therefore it is faster than Code Generated Java Beans because there I will have to access the getter/setter methods using reflection

Related

Why do we need a bean to bean mapper like dozer in a web application

In simple terms, why do we need 'a bean to bean mapping service' (like Dozer) in a web-application.
Suppose I'm working on a web-service.
I'm receiving an XML in request.
I fetch the the values from XML elements.
Perform the required operation on the fetched values.
Prepare the response XML.
Send the response XML as response
Why should I add one more steps of mapping XML elements to own custom elements.
I'm not able to convince myself, probably because I'm not able to think of a better situation/reason.
Please suggest, with example if possible.
It helps to reduce coupling between the presentation (i.e. the XML schema) and the business logic. For example in case of schema changes you don't have to touch the business logic, just the mapping between the objects.
In simple cases it might not be worth the additional complexity. But if the objects are used extensively in the business logic component you should consider it.
Just as a quick answer, the case you described is not the only one :).
Suppose you are working with an internal library providing some POJO / entity / other beans. You want to abstract from the internal representation (for a reason or anohter), you then want to map those bean to yours. It works :
for ejb client, or somehting like that,
when you don't want to expose internal entities (Business Object vs Presentation object) (see #Henry's reply)
you have beans that don't inherit from the same parent (and can't for any reason, even leacy) and you want to tarnsfert value from on to another
There are plenty of (other) reasons :)
As an advice see also orika
and this post :
any tool for java object to object mapping?
Short answer for me as henry said it helps reduce coupling between what you expose or consume and your core data model.
It is one way build Hexagonal Architecture. You can freely modify your core model without impacting the exposed model. In hexagonal architecture, it is used to expose only a small relevant part of the core model.
It is also a very goog way to handle services and model versionning since multiple versions can be mapped to the core model.
When working with XML services I tend to build contract first application so, I first write the XMLSchema then generate Jaxbeans and I realy don't want my business code to be polluted by JAxb annotations.
If you know that your exposed model will always be the same and that your application does not fall in the previously mentionned cases, so you realy don't need to use DTO.
Last, I would recommend using a framework with strong compile time checking like Selma instead of Dozer or Orika because they are evaluating the mapping only at runtime which is weak typing and sensible to refactoring.

Strategy for managing Java object serialization version when using cache

I have an application that uses JBoss Cache to cache some internal objects and the cache data is persisted in a MySQL database. When the application starts, data persisted in the database will be loaded into the cache. Java serialization mechanism is used to persist the data. I assigned a fixed value to serialVersionUID field for those persistent Java classes.
The problem is that when someone introduces incompatible changes to the serialization format, then the cache data loading from database will fail due to de-serialization errors. After some researches, I got some possible solutions, but not sure which one is the best.
Change the serialVersionUID to use a new version. This approach seems to be easy. But do I need to change all the classes' serialVersionUID field value to the new version, or just the actually changed classes? Because what's been read is actually an object graph, if different serialization versions are used for different classes, will that be a problem when de-serializing the whole object graph?
Use the same serialVersionUID value but create my own readObject and 'writeObject`.
Use Externalizable interface.
Which approach may be the best solution?
The first option is good enough for a cache system as an incompatible serialization change should be a rare event and should be treated as a cache miss. Basically you need to discard from cache the incompatible class instances and re-add the new ones. No need to change the serialVersionUID for compatible classes.
If you want to further minimize the number of incompatibilities between serializable class versions you might consider the second option to customize your class serialization form or provide a serialization proxy. Here you should think if a logical representation of the class is simpler than the default physical representation that might serialize unnecessary implementation details.
Providing a custom serialization form might have further important advantages for a cache system: the size of the serialization form might be reduced and the speed of serialization process might be increased.
See also "Effective Java" by Joshua Block that has a good chapter discussing serialization issues.

Why should I use EMF?

I've read some tutorials about EMF and I still wonder why I should use it.
Until now, I was generating my POJOs from XSD schema + JXC, or by hand.
As far as I understand EMF it can be useful to define some complex relationships between the classes (one-to-many, etc...). But is that all? Isn't it more complicated to generate the code with EMF? Doesn't it add some extra-dependencies?
In general terms one could say that using emf provides several benefits at runtime.
On a first stage you'll notice that the ecore classes (and the emf runtime) offer what you might need from your POJOs in your application. There's no further coding needed in a lot of areas whereas you would need to code a lot by hand when using plain POJOs:
You get a full blown notification system (no PropertyChange coding any more). It even offers notifications for changes that occur further down in you instance-tree (attach listener to x, get notified of changes in y which is referenced by x).
values are unsettable (actually a very common need: you need to know about 3 states of a value: it is set, it is set to null or it was not touched).
bi-directional references: X references Y and vice-versa. Remove the reference to Y in X, the opposite reference is removed, too.
XML, XMI (etc.) serialisation support out-of-the-box.
lazy loading: you can partition your model and have certain parts loading lazyly only.
etc.
Extensions to EMF even offer far more:
EMF Query or EMF Path add advanced query capabilities to collect pojo instances for given criterias
CDO allows you to code 3-tier applications without any further hand-coding. CDO adds Database persistency and remote notification (sessions, transactions, versioning, branching, etc.)
Xtext adds serialization to custom DSLs (define your own serialization format/dialect)
etc.
You could actually argue that EMF/Ecore offers a standard for POJO and a whole ecosystem grew around it that actually offers what you would code by hand in a classic approach.
To be honest, the downside of EMF is that you get tied to the Ecore runtime which is very fine if you code Eclipse based rich clients but may become an issue if you are on the server.
IF your only interest is generation of POJOs, then I agree that there are many alternatives out there to achive the same you can do with EMF.
However, Java generation was just the first application of EMF. Now there are a huge number of EMF-based Eclipse plug-ins that give your for free a lot of functionalities to manipulate (query, validate, transform,...) your EMF models.
See the Eclipse Modeling Project for a list of official Eclipse projects on EMF.
Also, take a look at Acceleo to see the flexibility of their template-based generation approach from EMF models (for Java, PHP,...).
Adding to what Jordi said EMF provides notification mechanism which unlike XML Beans for example, allows you to add listeners to model changes. So when changes occur in the model you get notified about this change.
I've successfully used EMF Query to search model using SQL-like syntax, and using OCL. EMF Validation is a great framework for validating your model based on what's defined in schema as well as introducing your own validation logic if it cannot be expressed in schema.
On high level EMF is an implementation of MOF standard for model-driven engineering or MDA. So, using EMF you get access from you generated Java code not to Java Objects, but to their Model too. Which if far more than just Java Reflection features.

How do I use JPA to make library objects database persistent?

I've been using JPA on a small application I've been working on. I now have a need to create a data structure that basically extends or encapsulates a graph data structure object. The graph will need to be persisted to the database.
For persistable objects I write myself, it is very easy to extend them and have the extending classes also persist easily. However, I now find myself wanting to use a library of graph related objects (Nodes, edges, simple graphs, directed graphs, etc) in the JGrahpT library. However, the base classes are not defined as persistable JPA objects, so I'm not sure how to get those classes to save into the database.
I have a couple ideas and I'd like some feedback.
Option 1)
Use the decorator design pattern as I go along to add persistence to an extended version of the base class.
Challenges:
-- How do I persist the private fields of a class that are needed for it to be in the correct state? Do I just extend the class add an ID field, and mark it as persistable? How will JPA get the necessary fields from the parent class? (Something like ruby's runtime class modification would be awesome here)
-- There is a class hierarchy (Abstract Graph, Directed Graph, Directed Weighted Graph, etc.). If I extend to get persistence, extending classes still won't have the common parent class. How do I resolve this? (Again, Something like ruby's runtime class modification would be awesome here)
Option 2) Copy paste the entire code base. Modify the source code of each file to make it JPA compatible.
-- obviously this is a lot of work
I'm sure there are other options.. What have you got for me SO???
Do the base classes follow the JavaBeans naming conventions? If so you should be able to map them using the XML syntax of JPA.
This is documented in Chapter 10 of the specification:
The XML descriptor is intended to
serve as both an alternative to and an
overriding mechanism for Java language
metadata annotations.
This XML file is usually called orm.xml. The schema is available online
Your options with JPA annotations seem pretty limited if you're working with a pre-existing library. One alternative would be to use something like Hibernate XML mapping files instead of JPA. You can declare your mappings outside of the classes themselves. Private fields aren't an issue, Hibernate will ignore access modifiers via reflection. However, even this may end up being more trouble than its worth depending on the internal logic of the code (Hibernate's use of special collections and proxies for instance, will get you in hot water if the classes directly access some of their properties instead of using getter methods internally).
On the other hand, I don't see why you'd consider option 2 'a lot of work'. Creating a ORM mapping isn't really a no brainer task no matter how you go about it, and personally I'd consider option 2 probably the least effort approach. You'd probably want to maintain it as a patch file so you could keep up with updates to the library, rather than just forking.

Versioned Serialization in Java

I have a simple Java class that I need to serialize to be stored as a value in an RDBMS or a key-value store. The class is just a collection of properties of simple types (native types or Maps/Lists of native types). The issue is that the class will likely be evolving over time (likely: adding new properties, less likely but still possible: renaming a property, changing the type of a property, deleting a property).
I'd like to be able to gracefully handle changes in the class version. Specifically, when an attempt is made to de-serialize an instance from an older version, I'd like to be able to specify some sort of handler for managing the transition of the older instance to the newest version.
I don't think Java's built-in serialization is appropriate here. Before I try to roll my own solution, I'm wondering if anyone knows of any existing libraries that might help? I know of a ton of alternative serialization methods for Java, but I'm specifically looking for something that will let me gracefully handle changes to a class definition over time.
Edit:
For what it's worth, I ended up going with Protocol Buffer (http://code.google.com/p/protobuf/) serialization, since it's flexible to adding and renaming fields, while being on less piece of code I have to maintain (in reference to the custom Java serialization using readObject/writeObject).
Java serialisation allows customising of the serial form by providing readObject and writeObject methods. Together with ObjectInputStream.readFields, ObjectOutputStrean.putFields and defining serialPersistentFields, the serialised form can be unrelated to the actual fields in the implementation class.
However, Java serialisation produces opaque data that is not amenable to reading and writing through other techniques.
Perhaps you should map your Java class into the relational model. Dumping some language serialized blob into a database column is a horrible approach.
This is pretty straightforward using read and write object.
Try setting serialversionuid to a fixed value, then define a static final field for your version. The readobject can then use a switch statement to construct the fields depending on the version. We use this to store historical data on our file system. It's very quick on retrieval- so much so that users can't tell the difference.
I had a similar problem. I found out Java's serialVersionUID doesn't help much when you have multiple versions of objects. So I rolled out my own.
Here is what I do to save our user sessions,
In my DB, besides the BLOB field for serialized objects, I added a version column.
Whenever we change the session object, I save the old class, for example SessionV3.
Session is always written to the DB with current version number.
When reading the session, it's deserialized into session object directly if version is current. Otherwise, it's deserialized into old object and manually copied into current session object (SessionV3 => Session).
Once a while, we run a DB script to remove real old session versions so we can clean out old sessions from code. If we care about the old sessions, we can choose to convert them also.
There might be easier way to do this but our approach gives us most flexibility.
Never tried it but you may be able to do something with a custom bootloader to load the correct version of the class file at runtime for the object being deserialized.

Categories