解组到内部静态类字段

时间:2014-08-04 17:08:51

标签: java jaxb moxy

我有一个带有复合键的实体类,它是实体的公共静态内部类。我想解组一个文件并将值放入内部类的字段中。

我尝试了几个@XmlKey,@ XmlPath,@ XmlJoinNode,但没有任何效果,我甚至不确定我是否正确。

我的xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<numbers>
    <one>one_text</one>
    <two>two_text</two>
    <three>three_text</three>
    <four>four_text</four>
</numbers>

我的课程:

@XmlRootElement(name = "numbers")
public class Numbers {

    public static class Id {

        @XmlElement
        private String one;

        @XmlElement
        private String two;

        public String getOne() {
            return one;
        }

        public void setOne(String one) {
            this.one = one;
        }

        public String getTwo() {
            return two;
        }

        public void setTwo(String two) {
            this.two = two;
        }
    }

    @XmlElement
    private String three;

    @XmlElement
    private String four;

    private Id id = new Id();

    public String getThree() {
        return three;
    }

    public void setThree(String three) {
        this.three = three;
    }

    public String getFour() {
        return four;
    }

    public void setFour(String four) {
        this.four = four;
    }

    public Id getId() {
        return id;
    }

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

不要担心错过JPA注释。谢谢你的回答。

更新

精度:不能修改XML文件,如果没有办法保留这样的东西,java类就可以了,但它必须可以被hibernate解释为具有复合主键的实体。

使用MOXy的解决方案

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "numbers")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
public class Numbers {

    @Embeddable
    public static class Id implements Serializable {

        private static final long serialVersionUID = -2153062768685935342L;

        @Column
        @XmlElement
        private String one;

        @Column
        @XmlElement
        private String two;

        public String getOne() {
            return one;
        }

        public void setOne(String one) {
            this.one = one;
        }

        public String getTwo() {
            return two;
        }

        public void setTwo(String two) {
            this.two = two;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((one == null) ? 0 : one.hashCode());
            result = prime * result + ((two == null) ? 0 : two.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Id other = (Id) obj;
            if (one == null) {
                if (other.one != null)
                    return false;
            } else if (!one.equals(other.one))
                return false;
            if (two == null) {
                if (other.two != null)
                    return false;
            } else if (!two.equals(other.two))
                return false;
            return true;
        }
    }

    @Column
    @XmlElement
    private String three;

    @Column
    @XmlElement
    private String four;

    @EmbeddedId
    @XmlPath(".")
    private Id id = new Id();


    public String getThree() {
        return three;
    }

    public void setThree(String three) {
        this.three = three;
    }


    public String getFour() {
        return four;
    }

    public void setFour(String four) {
        this.four = four;
    }

    public Id getId() {
        return id;
    }

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

替代MOXy

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement(name = "numbers")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
public class Numbers {

    @Embeddable
    public static class Id implements Serializable {

        private static final long serialVersionUID = -2153062768685935342L;

        @Column
        private String one;

        @Column
        private String two;

        public String getOne() {
            return one;
        }

        public void setOne(String one) {
            this.one = one;
        }

        public String getTwo() {
            return two;
        }

        public void setTwo(String two) {
            this.two = two;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((one == null) ? 0 : one.hashCode());
            result = prime * result + ((two == null) ? 0 : two.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Id other = (Id) obj;
            if (one == null) {
                if (other.one != null)
                    return false;
            } else if (!one.equals(other.one))
                return false;
            if (two == null) {
                if (other.two != null)
                    return false;
            } else if (!two.equals(other.two))
                return false;
            return true;
        }
    }

    @Column
    @XmlElement
    private String three;

    @Column
    @XmlElement
    private String four;

    @EmbeddedId
    @XmlTransient
    private Id id = new Id();


    public String getThree() {
        return three;
    }

    public void setThree(String three) {
        this.three = three;
    }


    public String getFour() {
        return four;
    }

    public void setFour(String four) {
        this.four = four;
    }

    public Id getId() {
        return id;
    }

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

    @XmlElement
    public String getOne() {
        return id.getOne();
    }

    public void setOne(String one) {
        id.setOne(one);
    }

    @XmlElement
    public String getTwo() {
        return id.getTwo();
    }

    public void setTwo(String two) {
        id.setTwo(two);
    }
}

2 个答案:

答案 0 :(得分:1)

由于您的问题被标记为[moxy],因此您可以使用MOXy的@XmlPath扩展名来获得所需的行为。

@XmlPath(".")
private Id id = new Id();

答案 1 :(得分:0)

可以(un)编组您的类Numbers的XML是这样的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<numbers>
  <three>tres</three>
  <four>quattro</four>
  <id>
    <one>uno</one>
    <two>due</two>
  </id>
</numbers>

这是Numbers,添加了一些注释,省略了之前的相同内容:

@XmlRootElement(name = "numbers")
@XmlAccessorType(XmlAccessType.FIELD)
public class Numbers {

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Id {
        @XmlElement
        private String one;
        @XmlElement
        private String two;
        // getters, setters
     }

    @XmlElement
    private String three;
    @XmlElement
    private String four;
    @XmlElement
    private Id id = new Id();
    // getters, setters
}

实现

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<numbers>
  <four>quattro</four>
  <one>uno</one>
  <three>tres</three>
  <two>due</two>
</numbers>

您可以将所有字段都放入Numbers:

@XmlRootElement(name = "numbers")
@XmlAccessorType(XmlAccessType.FIELD)
public class Numbers {
    @XmlElement
    private String one;
    @XmlElement
    private String two;
    @XmlElement
    private String three;
    @XmlElement
    private String four;
    // getters, setters
}

或者您可以委派:

@XmlRootElement(name = "numbers")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Numbers {

public static class Id {
    private String one;
    private String two;
    // getters, setters
}

private String three;
private String four;
private Id id = new Id();

@XmlElement
public String getThree() {
    return three;
}
public void setThree(String three) {
    this.three = three;
}
@XmlElement
public String getFour() {
    return four;
}
public void setFour(String four) {
    this.four = four;
}
@XmlTransient
public Id getId() {
    return id;
}
public void setId(Id id) {
    this.id = id;
}    
@XmlElement
public String getOne() {
    return id.getOne();
}
public void setOne(String one) {
    id.setOne( one );
}
@XmlElement
public String getTwo() {
    return id.getTwo();
}
public void setTwo(String two) {
    id.setTwo( two );
}
}