JPA CDI JAXB - Setter被忽略了

时间:2014-04-30 23:08:34

标签: java java-ee jpa-2.0 java-ee-6 openjpa

我的JAX-RS资源成功从db获取JPA / JAXB实体和JPA / JAXB实体列表。

一个实体充当父实体。实体列表是父实体中的字段。我无法将父实体列表设置为返回的实体列表。父实体在JAXB父实体中返回,但这不会影响该情况。

以下是代码:

@Inject
InventoryService inventoryService;
@Inject
HcUser user;
@Inject
InventoryResponse inventoryResponse;

@GET
@Produces(MediaType.APPLICATION_JSON)
public InventoryResponse getInventory(@Context HttpServletRequest request, 
                                      @HeaderParam(IDENTITY_URL) String identityURL, 
                                      @HeaderParam(ACCESS_TOKEN) String accessToken) {

    String username = (String) request.getAttribute("username");
    user = inventoryService.getUserById(username);
    user.setHcCounts(inventoryService.getCountsForUserId(username));
    inventoryResponse.setUser(user);

    return inventoryResponse;
}

返回的JSON仅返回用户对象。我尝试手动实例化用户对象并将其设置为getUserById方法的返回值,然后使用返回的列表调用setHcCounts。但是,设置器仍然被忽略。

我做错了什么?

我正在使用WAS v8.0.0.8。堆栈是:

JAX-RS - Apache Wink v1.1.1(由WAS 8提供)

OpenJPA - Apache v2.1.2-SNAPSHOT(由WAS 8提供)

JAXB - MOXy v2.7

CDI - Apache OpenWebBeans 1.0(由WAS 8提供)

EJB - Apache OpenEJB(由WAS 8提供)

更新1 这是InventoryResponse课程的要求,但我不认为这是必要的。在检查用户对象时,在inventoryResonse.setUser(user)之前的行上,在调试期间,hcCounts为空。

@Named
@RequestScoped
@XmlRootElement
public class InventoryResponse implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private HcUser user;

    private List<HcLocation> locations;

    public HcUser getUser() {
        return user;
    }

    public void setUser(HcUser user) {
        this.user = user;
    }

    public List<HcLocation> getLocations() {
        return locations;
    }

    public void setLocations(List<HcLocation> locations) {
        this.locations = locations;
    }
}

更新2 根据要求,HcUser:

import java.io.Serializable;

import javax.inject.Named;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import java.util.Date;
import java.util.List;


@Entity
@Table(schema="<ommitted>", name="<ommitted>")
@Named
@XmlRootElement
public class HcUser implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false, length=100)
private String id;

@Column(nullable=false, length=1)
private boolean active;

@Temporal(TemporalType.DATE)
@Column(name="CREATE_DATE")
private Date createDate;

@Column(name="FIRST_NAME", length=100)
private String firstName;

@Column(name="LAST_NAME", length=100)
private String lastName;

@Temporal(TemporalType.DATE)
@Column(name="UPDATE_DATE")
private Date updateDate;

//bi-directional many-to-one association to HcAssignment
@OneToMany(mappedBy="hcUser")
@XmlElement
private List<HcAssignment> hcAssignments;

//bi-directional many-to-one association to HcCount
@OneToMany(mappedBy="hcUser")
@XmlElement
private List<HcCount> hcCounts;

public HcUser() {
}

public String getId() {
    return this.id;
}

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

public boolean getActive() {
    return this.active;
}

public void setActive(boolean active) {
    this.active = active;
}

public Date getCreateDate() {
    return this.createDate;
}

public void setCreateDate(Date createDate) {
    this.createDate = createDate;
}

public String getFirstName() {
    return this.firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return this.lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public Date getUpdateDate() {
    return this.updateDate;
}

public void setUpdateDate(Date updateDate) {
    this.updateDate = updateDate;
}

public List<HcAssignment> getHcAssignments() {
    return this.hcAssignments;
}

public void setHcAssignments(List<HcAssignment> hcAssignments) {
    this.hcAssignments = hcAssignments;
}

public HcAssignment addHcAssignment(HcAssignment hcAssignment) {
    getHcAssignments().add(hcAssignment);
    hcAssignment.setHcUser(this);

    return hcAssignment;
}

public HcAssignment removeHcAssignment(HcAssignment hcAssignment) {
    getHcAssignments().remove(hcAssignment);
    hcAssignment.setHcUser(null);

    return hcAssignment;
}

public List<HcCount> getHcCounts() {
    return this.hcCounts;
}

public void setHcCounts(List<HcCount> hcCounts) {
    this.hcCounts = hcCounts;
}

public HcCount addHcCount(HcCount hcCount) {
    getHcCounts().add(hcCount);
    hcCount.setHcUser(this);

    return hcCount;
}

public HcCount removeHcCount(HcCount hcCount) {
    getHcCounts().remove(hcCount);
    hcCount.setHcUser(null);

    return hcCount;
}

/* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}

/* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof HcUser)) {
        return false;
    }
    HcUser other = (HcUser) obj;
    if (id == null) {
        if (other.id != null) {
            return false;
        }
    } else if (!id.equals(other.id)) {
        return false;
    }
    return true;
}
}

更新3 这是HcCount的代码:

import java.io.Serializable;

import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

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

import java.math.BigDecimal;
import java.util.Date;

@Entity
@Table(schema="<omitted>", name="<omitted>")
@Named
@XmlRootElement
public class HcCount implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@SequenceGenerator(name="HC_COUNT_ID_GENERATOR", sequenceName="COUNT_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="HC_COUNT_ID_GENERATOR")
@Column(unique=true, nullable=false)
private long id;

@Column(name = "LOCATION_NUM", nullable = false, length = 100)
private String locationNum;

@Column(name = "PRODUCT_CODE", nullable = false, length = 100)
private String productCode;

@Column(name = "USER_ID", nullable = false, length = 100)
private String userId;

@Column(name = "LOT_CODE", nullable = false, length = 100)
private String lotCode;

@Column(name="\"COUNT\"")
private BigDecimal count;

@Temporal(TemporalType.DATE)
@Column(name="COUNT_DATE", unique=true, nullable=false)
private Date countDate;

@Temporal(TemporalType.DATE)
@Column(name="CREATE_DATE")
private Date createDate;

@Temporal(TemporalType.DATE)
@Column(name="UPDATE_DATE")
private Date updateDate;

//bi-directional many-to-one association to HcUser
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="USER_ID", unique=true, nullable=false)
@XmlElement
@XmlInverseReference(mappedBy="hcCounts")
@Inject private HcUser hcUser;

//bi-directional many-to-one association to HcLocation
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="LOCATION_NUM", referencedColumnName="NUM", unique=true, nullable=false)
@XmlElement
@XmlInverseReference(mappedBy="hcCounts")
@Inject private HcLocation hcLocation;

//bi-directional many-to-one association to HcProduct
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PRODUCT_CODE", referencedColumnName="CODE_ID", unique=true, nullable=false)
@XmlElement
@XmlInverseReference(mappedBy="hcCounts")
@Inject private HcProduct hcProduct;

//bi-directional many-to-one association to HcLot
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="LOT_CODE", referencedColumnName="CODE_ID", unique=true, nullable=false)
@XmlElement
@XmlInverseReference(mappedBy="hcCounts")
@Inject private HcLot hcLot;

public HcCount() {
}

public long getId() {
    return this.id;
}

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

public String getLocationNum() {
    return locationNum;
}

public void setLocationNum(String locationNum) {
    this.locationNum = locationNum;
}

public String getProductCode() {
    return productCode;
}

public void setProductCode(String productCode) {
    this.productCode = productCode;
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getLotCode() {
    return lotCode;
}

public void setLotCode(String lotCode) {
    this.lotCode = lotCode;
}

public BigDecimal getCount() {
    return this.count;
}

public void setCount(BigDecimal count) {
    this.count = count;
}

public Date getCountDate() {
    return this.countDate;
}

public void setCountDate(Date countDate) {
    this.countDate = countDate;
}

public Date getCreateDate() {
    return this.createDate;
}

public void setCreateDate(Date createDate) {
    this.createDate = createDate;
}

public Date getUpdateDate() {
    return this.updateDate;
}

public void setUpdateDate(Date updateDate) {
    this.updateDate = updateDate;
}

public HcUser getHcUser() {
    return this.hcUser;
}

public void setHcUser(HcUser hcUser) {
    this.hcUser = hcUser;
}

public HcLocation getHcLocation() {
    return this.hcLocation;
}

public void setHcLocation(HcLocation hcLocation) {
    this.hcLocation = hcLocation;
}

public HcProduct getHcProduct() {
    return this.hcProduct;
}

public void setHcProduct(HcProduct hcProduct) {
    this.hcProduct = hcProduct;
}

public HcLot getHcLot() {
    return this.hcLot;
}

public void setHcLot(HcLot hcLot) {
    this.hcLot = hcLot;
}

/* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + (int) (id ^ (id >>> 32));
    return result;
}

/* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof HcCount)) {
        return false;
    }
    HcCount other = (HcCount) obj;
    if (id != other.id) {
        return false;
    }
    return true;
}
}

更新4 我找到了一个解决方法......

public InventoryResponse getInventory(@Context HttpServletRequest request, @HeaderParam(IDENTITY_URL) String identityURL, @HeaderParam(ACCESS_TOKEN) String accessToken) {
    String username = (String) request.getAttribute("username");
    user = inventoryService.getUserById(username);
    List<HcCount> counts = inventoryService.getCountsForUserId(username);
    HcUser newUser = new HcUser();
    newUser.setHcCounts(counts);
    inventoryResponse.setUser(newUser);

    return inventoryResponse;
}

0 个答案:

没有答案