(moxy)jaxb封送处理和休眠代理对象

(moxy)jaxb封送处理和休眠代理对象,第1张

(moxy)jaxb封送处理和休眠代理对象

要解决此Hibernate问题,您可以使用

XmlAdapter
。XmlAdapter看起来类似于marshal方法中的逻辑将从代理转换为真实对象的地方:

package forum6838323;import javax.xml.bind.annotation.adapters.XmlAdapter;public class AddressAdapter extends XmlAdapter<Address, Address> {    @Override    public Address unmarshal(Address v) throws Exception {        return v;    }    @Override    public Address marshal(Address v) throws Exception {        // TODO Auto-generated method stub        return null;    }}

您可以

XmlAdapter
如下配置:

public class User {    @ManyToOne(fetch = FetchType.LAZY)    @JoinColumn(name = "address")    @XmlJavaTypeAdapter(AddressAdapter.class)    public Address getAddress() {        return address;    }}

使用Eclipselink JPA的替代方法

注意:Eclipselink JPA中的延迟加载不会导致此问题:

用户

package forum6838323;import javax.persistence.*;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlRootElement;@Entity@Table(name="users")@XmlRootElementpublic class User  {    private int id;    Address address;    @Id    @XmlAttribute    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    @ManyToOne(fetch = FetchType.LAZY)    @JoinColumn(name = "address")    public Address getAddress() {        return address;    }    public void setAddress(Address address) {        this.address = address;    }}

地址

package forum6838323;import javax.persistence.*;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlSeeAlso;@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING)@DiscriminatorValue("ADDRESS")@XmlSeeAlso(CoolAddress.class)public class Address {    private int id;    private String street;    @Id    @XmlAttribute    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getStreet() {        return street;    }    public void setStreet(String street) {        this.street = street;    }}

酷地址

package forum6838323;import javax.persistence.*;@Entity@DiscriminatorValue("COOL")public class CoolAddress extends Address {    private String something;    public String getSomething() {        return something;    }    public void setSomething(String something) {        this.something = something;    }}

演示版

package forum6838323;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import javax.xml.bind.JAXBContext;import javax.xml.bind.Marshaller;public class Demo {    public static void main(String[] args) throws Exception {        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Forum6838323");        EntityManager em = emf.createEntityManager();        User user = em.find(User.class, 2);        System.out.println("user.address BEFORE marshal:  " + user.address);        JAXBContext jc = JAXBContext.newInstance(User.class);        Marshaller marshaller = jc.createMarshaller();        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);        marshaller.marshal(user, System.out);        System.out.println("user.address AFTER marshal:  " + user.address);    }}

输出量

您可以从输出中看到地址值被延迟加载,因为该字段在编组之前为null,然后在该字段之后填充:

user.address BEFORE marshal:  null[EL Finest]: 2011-07-27 11:47:13.118--ServerSession(23503403)--Thread(Thread[main,5,main])--Execute query ReadObjectQuery(name="Forum6838323" referenceClass=Address )[EL Finest]: 2011-07-27 11:47:13.118--ServerSession(23503403)--Connection(10272075)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].[EL Fine]: 2011-07-27 11:47:13.118--ServerSession(23503403)--Connection(10272075)--Thread(Thread[main,5,main])--SELECt ID, TYPE, STREET, SOMETHING FROM ADDRESS WHERe (ID = ?)    bind => [2][EL Finest]: 2011-07-27 11:47:13.118--ServerSession(23503403)--Connection(10272075)--Thread(Thread[main,5,main])--Connection released to connection pool [default].[EL Finest]: 2011-07-27 11:47:13.118--UnitOfWork(6131844)--Thread(Thread[main,5,main])--Register the existing object forum6838323.CoolAddress@109ea96<?xml version="1.0" encoding="UTF-8" standalone="yes"?><user id="2">    <address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="coolAddress" id="2">        <street>2 B Road</street>        <something>Cool Road</something>    </address></user>user.address AFTER marshal:  forum6838323.CoolAddress@83b1b


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5163190.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存