Hibernate:将toString()类方法映射到String db列

时间:2014-09-10 08:33:40

标签: java spring hibernate tostring

我有2个A类和C类

@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){ ... }
}

和像这样的db表

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

由于toString()方法,我希望将整个C元素存储在c列中,并将其从数据库thx读取到fromString()。 有没有办法通过hibernate映射轻松实现它?

3 个答案:

答案 0 :(得分:1)

最简单的方法是在A类中定义getter / setter,它将调用C.toString()和C.fromString(),如下所示:

@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);
    }        
}

注意:使用@Transient注释标记getC(),因为它不打算映射到任何列。

答案 1 :(得分:0)

查看2 minute tutorial on XStream

最简单的方法是将班级cA字段的类型从C更改为String

然后使用其余代码中的XStream将C的对象转换为XML字符串,并在从C类型的对象获取它时将其转换回A

编辑:Hibernate自定义类型是在数据库中序列化C的字符串编码版本的方法。看一下本教程:http://blog.xebia.com/2009/11/09/understanding-and-writing-hibernate-user-types/

答案 2 :(得分:0)

我建议您使用JAXB ..您可以看到this教程。

在newInstance中,您必须添加映射xml的类根元素...在示例

下面

这是一个例子..

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>