2

I have 2 classes A and C of this kind

@Entity
@Table(name = "A")
public class A
{
    private int id;
    private String b;
    private C c;

    // Getters & Setters with @Column annotation
}

public class C
{
    private String d;
    private String e;

    // Getters & Setters ...

    public String toString() {...}
    public boolean fromString(String serializedC){ ... }
}

and a db table like this

CREATE TABLE A
{
    INT id NOT NULL,
    VARCHAR(64) b,
    TEXT c
};

I would like to store the whole C element within c column thanks to the toString() method and read it from the database thx to the fromString() one. Is there any way to achieve it easily with an hibernate mapping?

3 Answers 3

1

The easiest way is to define getter/setter in class A that will call C.toString() and C.fromString() like so:

@Entity
@Table(name = "A")
public class A {

    private int id;
    private String b;
    private C c;

    @Id
    @Column
    public int getId() {
        return id;
    }

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

    @Column
    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    @Transient
    public C getC() {
        return c;
    }
    public void setC(C c) {
        this.c = c;
    }

    @Column
    protected String getCAsString() {
        return c!=null? c.toString() : null;
    }
    protected void setCAsString(String c) {
        this.c = new C();
        this.c.fromString(c);
    }        
}

Note: mark getC() with @Transient annotation as it is not intended to be mapped to any column.

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

Comments

0

I suggest you JAXB.. you can see this tutorial .

In newInstance you must add the class root element that map your xml... below an example

Here an example ..

public static void main(String[] args) throws JAXBException {
        final JAXBContext context = JAXBContext.newInstance(Vehicals.class);
        final Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        final Vehicals vehicals = new Vehicals();

        List<Car> cars = new ArrayList<Car>();
        Car c = new Car();
        c.setName("Mercedes");
        cars.add(c);

        c = new Car();
        c.setName("BMW");
        cars.add(c);

        vehicals.setCar(cars);

        m.marshal(vehicals, System.out);
    }

Vehicals.java

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class Vehicals {

        private List<Car> Car;

        public List<Car> getCar() {
            return Car;
        }

        public void setCar(List<Car> cars) {
            this.Car = cars;
        }
    }

Car.java

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

@XmlRootElement
public class Car {

    @XmlTransient
    private Long id;

    private String name;

    @XmlTransient
    private String code;


    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
}

output.xml

<Vehicle>
     <Car>
         <name>Mercedes</name>
      </Car> 
     <Car>
         <name>BMW</name>
     </Car>
</Vehicle>

Comments

0

Take a look at the 2 minute tutorial on XStream

The easiest thing is to change the type of the c field in the class A from C to String

Then use the XStream from the rest of the code to transform an object of C to XML string and transform it back to C when getting it from an object of type A.

EDIT: Hibernate Custom Types are the way to go to serialize a string encoded version of C in a database. Take a look at this tutorial: http://blog.xebia.com/2009/11/09/understanding-and-writing-hibernate-user-types/

1 Comment

Actually, in my mind I would use xstream inside toString() and fromString() methods of class C. There's no easy way to tell Hibernate to use C.toString() and C.fromString() methods while mapping C to the TEXT db column?

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.