2

Assume a Java object:

Object obj = new Object();

How do I store this object in the database so to persist this status, and after I store it, it should be easy to convert to a Java object like when I store it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tsaowe
  • 45
  • 1
  • 4

5 Answers5

5

Any of these will do:

Or just plain JDK XMLEncoder / XMLDecoder (introductory article), but that's a royal pain in the donkey if you ask me.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    Have you tried JAXB? Here are a couple of articles I wrote comparing with Simple (http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-simple.html), and XStream (http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-xstream.html). – bdoughan Aug 24 '11 at 10:10
  • @Blaise I was going to add JAXB, but bigGuy had already done that. Personally, I wouldn't use any of these, XML mapping is just not my thing :-) – Sean Patrick Floyd Aug 24 '11 at 10:15
4

Also, JAXB. (Hello World example)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
bigGuy
  • 1,732
  • 1
  • 22
  • 37
2

You can use XStream which is very easy and straightforward as shown in this 2 minute tutorial.

npinti
  • 51,780
  • 5
  • 72
  • 96
2

Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.


How to convert a Java object to an XML string, and conversely convert XML to a Java object?

With JAXB you can marshal an Object to a java.io.StringWriter to produce a String, and leverage a java.io.StringReader to unmarshal an Object from a String.


then I want to store this object in the database

The Java EE solution is to use a JPA implementation (TopLink/EclipseLink, Hibernate, Open JPA, etc.) to store the object in the database, and a JAXB implementation (TopLink/EclipseLink, Metro, Apache JaxMe, etc.) to convert the object to/from XML.

Since you will be persisting your objects to a database you need to be sure that your object-to-XML mapping (OXM) solution takes the following things into consideration:

Bidirectional Relationships

Object models used with object-to-relational mapping (ORM) solutions often have bidirectional relationships. You need to ensure this doesn't send your OXM layer into a loop. MOXy JAXB for example has the @XmlInverseReference extension to handle this:

Lazy Relationships

Many ORM solutions offer a mechanism to to have relationships between objects lazily realized. Some like TopLink/EclipseLink modify the Java bytecodes to add the logic to the "get" methods. You will need an OXM solution that is able to leverage both fields and properties. In MOXy or any JAXB implementation this is done via @XmlAccessorType:

Compound Keys/Embedded Key Classes

Database tables often have compound keys. If these keys will also be leveraged for intra-document references then you will need an OXM solution that can handle it. MOXy offers @XmlKey and @XmlJoinNodes:

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
0

Here is the simplest and fastest code by abacus-common:

Account account = N.fill(Account.class);
String xml = N.toXML(account);
N.println(xml); // <account><id>6264304841028291043</id><gui>33acdcbe-fd5b-49</gui><emailAddress>19c1400a-97ae-43</emailAddress><firstName>67922557-8bb4-47</firstName><middleName>7ef242c9-8ddf-48</middleName><lastName>1ec6c731-a3fd-42</lastName><birthDate>1480444055841</birthDate><status>1444930636</status><lastUpdateTime>1480444055841</lastUpdateTime><createTime>1480444055841</createTime></account>
Account account2 = N.fromXML(Account.class, xml);
assertEquals(account, account2);

Declaration: I'm the developer of abacus-common.

user_3380739
  • 1
  • 14
  • 14