0

I'm using Jackson to map JSON data(that I retrieve from an Internet server) to application objects (e.g. Artist,Clip,Playlist). Most of the times the data is static and hence I don't need to load it excessively - so I'd like to store it in a database. I need a similar to Jackson mapper which will parse the database output and construct the objects (e.g. Artist,Clip,Playlist) for me. Is there any ?

Thanks.

midnight
  • 3,420
  • 3
  • 36
  • 58

2 Answers2

1

What you're looking for is an ORM (Object-Relational Mapping) library. There's a good discussion of ORM solutions in this post.

Community
  • 1
  • 1
acj
  • 4,821
  • 4
  • 34
  • 49
1

One way to solve this problem using the below solution.

You can use object-relationship mapping (ORM) technique (Example As Hibernate, TopLink) for mapping database table to Java Object. For parsing from Java Object to JSON Object , you can use either JAXB annotation. JaxB Annotation Class and Marshal Example as below.

JaxB Annotation Java Example

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class UserJaxB {

private String name;
private String id;

public UserJaxB() {
}

@XmlElement(name = "id")
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

@XmlElement(name = "name")
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

Marshaling Example

import java.io.*;
import javax.xml.bind.*;
import javax.xml.stream.XMLStreamWriter;
import org.codehaus.jettison.mapped.*;

public class MarshalDemo {

public static void main(String[] args) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(UserJaxB.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    UserJaxB userJaxB = new UserJaxB();
    userJaxB.setId("123");
    userJaxB.setName("ravi");

    Configuration config = new Configuration();
    MappedNamespaceConvention con = new MappedNamespaceConvention(config);
    Writer writer = new OutputStreamWriter(System.out);
    XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.marshal(userJaxB, xmlStreamWriter);
}

}
R H
  • 387
  • 3
  • 13