JPA:如何将一个实体的属性值映射到另一个实体的集合属性

时间:2017-05-03 01:45:02

标签: jpa hibernate-mapping openxava

我有两个实体,命名为Sport和Image。

如上所述,如何将Sport的sportId属性值映射到Image的@ManyToOne集合属性?例如,我希望sportId(例如1)的值显示在image的sportId的collection属性字段中。我怎样才能做到这一点?

Sport.java

@Views({@View(members= "title; date; estimatedCost; attendance; remark; images"), 

@View(name="NoImagesCollection", members= "sportId; title; date; estimatedCost; attendance; remark")})
@Entity
public class Sport {

    //******************************FORM ID******************************//
    @Id 
    @Hidden
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="SPORT_ID", length=10, unique = true, nullable = false, updatable = false)
    private int sportId;

    @NoCreate
    @NoModify
    @OneToMany(fetch = FetchType.LAZY,cascade=CascadeType.ALL,mappedBy="sports")
    private Collection<Image> images;

    //******************************TITLE******************************//
    @Column(name="SPORT_TITLE", precision=2000)
    @Required
    private String title;

    //******************************DATE START******************************//
   // @Stereotype("DATE")
    @Column(name="SPORT_DATE")
    @Required
    private Date date;

    //******************************ESTIMATED COST******************************//
    @Hidden
    @Stereotype("MONEY")
    @Column(name="SPORT_EST_COST")
    @Required
    private BigDecimal estimatedCost; // Include the import java.math.*  BigDecimal is typically used for money

    //******************************ESTIMATED ATTENDEES******************************//
    @Hidden
    @Column(name="SPORT_ATTENDANCE", length=10)
    @Required
    private int attendance;

    //******************************REMARK******************************//
    @Hidden
    @Editor("TextAreaNoFrame")
    @Stereotype("MEMO")
    @Column(name="SPORT_REMARK", precision=2000)
    private String remark;

    //******************************ENTERED DATE******************************//
    @Hidden
    @Column(name="ENTERED_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date enteredDate;

    @PrePersist
    private void setCreateDate() {
        enteredDate = new Date();
    }

    //******************************MODIFIED DATE******************************//
    @Hidden
    @Column(name="MODIFIED_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date modifiedDate;

    @PostUpdate
    private void updateModifyDate() {
        modifiedDate = new Date();
    }

  //******************************GETTERS AND SETTERS FOR ALL PROPERTIES******************************//

    public int getSportId() {
        return sportId;
    }

    public void setSportId(int sportId) {
        this.sportId = sportId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public BigDecimal getEstimatedCost() {
        return estimatedCost;
    }

    public void setEstimatedCost(BigDecimal estimatedCost) {
        this.estimatedCost = estimatedCost;
    }

    public int getAttendance() {
        return attendance;
    }

    public void setAttendance(int attendance) {
        this.attendance = attendance;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Date getEnteredDate() {
        return enteredDate;
    }

    public void setEnteredDate(Date enteredDate) {
        this.enteredDate = enteredDate;
    }

    public Date getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(Date modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public Collection<Image> getImages() {
        return images;
    }

    public void setImages(Collection<Image> images) {
        this.images = images;
    }

}

Image.java

@View(members="sports; image")

@Entity
public class Image {                     

    @Id
    @Hidden 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name="IMG_ID", unique = true, nullable = false, updatable = false)
    private int imgId;

    @Required
    @Column(name="IMG_IMAGE")
    @Stereotype("PHOTO")
    private byte [] image;

    @ReferenceView("NoImagesCollection")
    @Required
    @NoFrame
    @ManyToOne(optional=true)
    @JoinColumn(name="IMG_SPORT_ID", nullable = false)
    private Sport sports;

    @Hidden
    @Column(name="ENTERED_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date penteredDate;

    @PrePersist
    private void setCreateDate() {
        penteredDate = new Date();
    }

    @Hidden
    @Column(name="MODIFIED_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date pmodifiedDate;

    @PostUpdate
    private void updateModifyDate() {
        pmodifiedDate = new Date();
    }

    public int getImgId() {
        return imgId;
    }

    public void setImgId(int imgId) {
        this.imgId = imgId;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public Sport getSports() {
        return sports;
    }

    public void setSports(Sport sports) {
        this.sports = sports;
    }

    public Date getPenteredDate() {
        return penteredDate;
    }

    public void setPenteredDate(Date penteredDate) {
        this.penteredDate = penteredDate;
    }

    public Date getPmodifiedDate() {
        return pmodifiedDate;
    }

    public void setPmodifiedDate(Date pmodifiedDate) {
        this.pmodifiedDate = pmodifiedDate;
    }

}

ListSportImagesAction.java

public class ListSportImagesAction extends TabBaseAction implements IForwardAction { 

private int row;

@Inject  
private Tab tab;

public void execute() throws Exception {
    Map sportKey = (Map) tab.getTableModel().getObjectAt(row);         
    int sportId = ((Integer) sportKey.get("sportId")).intValue();
    Tab imageTab = (Tab)getContext().get("CkSurvey", getForwardURI(), "xava_tab");           
    imageTab.setBaseCondition("${sport.sportId} = " + sportId);
    System.out.println("id================="+sportId);
}

public int getRow() {                                                   
    return row;
}
public void setRow(int row) {                                          
    this.row = row;
}

public Tab getTab() {
    return tab;
}
public void setTab(Tab tab) {
    this.tab = tab;
}

@Override
public String getForwardURI() {
    return "/m/Image";
}

@Override
public boolean inNewWindow() {
    return true;
}

}

我们将不胜感激。

- 编辑 -

我添加了映射值的操作的代码。虽然它显示在Eclipse的控制台上,但我无法在属性字段中显示该值。

0 个答案:

没有答案