如何将数据插入此Entity对象?

时间:2013-03-15 12:16:29

标签: java entity

这是我的实体类

package entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "RESERVATION")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Reservation.findAll", query = "SELECT r FROM Reservation r"),
    @NamedQuery(name = "Reservation.findByReservationid", query = "SELECT r FROM Reservation r WHERE r.reservationid = :reservationid"),
    @NamedQuery(name = "Reservation.findByReservationdate", query = "SELECT r FROM Reservation r WHERE r.reservationdate = :reservationdate")})
public class Reservation implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "RESERVATIONID")
    private String reservationid;
    @Column(name = "RESERVATIONDATE")
    @Temporal(TemporalType.DATE)
    private Date reservationdate;
    @OneToMany(mappedBy = "loginid")
    private List<Reservation> reservationList;
    @JoinColumn(name = "LOGINID", referencedColumnName = "LOGINID")
    @ManyToOne
    private Reservation loginid;

    public Reservation() {
    }

    public Reservation(String reservationid) {
        this.reservationid = reservationid;
    }

    public String getReservationid() {
        return reservationid;
    }

    public void setReservationid(String reservationid) {
        this.reservationid = reservationid;
    }

    public Date getReservationdate() {
        return reservationdate;
    }

    public void setReservationdate(Date reservationdate) {
        this.reservationdate = reservationdate;
    }

    @XmlTransient
    public List<Reservation> getReservationList() {
        return reservationList;
    }

    public void setReservationList(List<Reservation> reservationList) {
        this.reservationList = reservationList;
    }

    public Reservation getLoginid() {
        return loginid;
    }

    public void setLoginid(Reservation loginid) {
        this.loginid = loginid;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (reservationid != null ? reservationid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {

        if (!(object instanceof Reservation)) {
            return false;
    }
        Reservation other = (Reservation) object;
        if ((this.reservationid == null && other.reservationid != null) || (this.reservationid != null && !this.reservationid.equals(other.reservationid))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "entity.Reservation[ reservationid=" + reservationid + " ]";
}

}

我应该如何将数据插入此表? 我的桌子

ReservationID VARCHAR(10)
loginid VARCHAR(20)(来自成员表的外键)
TourID VARCHAR(10)(来自旅游包表的外键)
ReservationDate DATE

1 个答案:

答案 0 :(得分:0)

您不确定您使用的是框架,只需查看实体

即可

如果您使用的是EJB3

您可以使用EntityManager并调用Persist(entity)方法来保存实体

如果您正在使用原始Hibernate

获取Session并使用save方法

会话 - &gt;的org.hibernate.Session