我正在使用Hibernate一对多的关系来保存和获取我的数据,读取数据时可以获取机构值,但是在保存机构值到数据库时无法插入。
有人能帮我知道我做错了什么吗?
在我的jsp中,我使用以下代码来显示机构下拉列表:
<div class = "clearfix"></div>
<div class="form-group">
<label for="agency" class="col-sm-2 control-label">Agency</label>
<div class="col-sm-10">
<form:select path="agencies" class = "form-control">
<c:forEach items="${agencies}" var="agency">
<option value="${agency.agency_id}" >${agency.name}</option>
</c:forEach>
</form:select>
</div>
</div>
我的活动实体就像
package com.nativeunlock.dto;
import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.annotations.Parameter;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import org.hibernate.annotations.GenericGenerator;
import lombok.Getter;
import lombok.Setter;
@SuppressWarnings("serial")
@NamedQueries({
@NamedQuery(
name = CampaignEntity.GET_CAMPAIGNS_QUERY,
query = "from CampaignEntity campaign"
),
@NamedQuery(
name = CampaignEntity.DELETE_CAMPAIGNS_QUERY,
query = "DELETE FROM CampaignEntity campaign WHERE campaign.campaign_id = :campaign_id"
),
@NamedQuery(
name = CampaignEntity.GET_CAMPAIGN_QUERY,
query = "FROM CampaignEntity campaign WHERE campaign.campaign_id = :campaign_id"
)
})
@Entity
@Table(name = "campaign")
public class CampaignEntity implements Serializable {
public static final String GET_CAMPAIGNS_QUERY ="getCampaignList";
public static final String DELETE_CAMPAIGNS_QUERY ="deleteCampaignList";
public static final String GET_CAMPAIGN_QUERY = "getCampaign";
@Id
@Getter
@Setter
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "campaign_id", unique = true, nullable = false)
private int campaign_id;
@Getter
@Setter
@Column(name = "name", unique = true)
//@NotEmpty(message="Name field is mandatory.")
private String name;
@Getter
@Setter
@Column(name = "no_of_views")
//@NotNull(message="No. of Views field is mandatory.")
private int no_of_views;
@Getter
@Setter
@Column(name = "video_url")
//@NotEmpty(message="Video URL field is mandatory.")
private String video_url;
@Getter
@Setter
@Column(name = "start_date")
//@NotEmpty(message="Start Date field is mandatory.")
private String start_date;
@Getter
@Setter
@Column(name = "end_date")
//@NotEmpty(message="End Date field is mandatory.")
private String end_date;
@Getter
@Setter
@Column(name = "click_to_play")
//@NotNull(message="Click to play field is mandatory.")
private int click_to_play;
@Getter
@Setter
@Column(name = "frequency")
//@NotNull(message="Frequency field is mandatory.")
private int frequency;
@Getter
@Setter
@Column(name = "priority")
@Digits(fraction = 0, integer = 100)
//@NotNull(message="Priority field is mandatory.")
private int priority;
@Getter
@Setter
@Column(name = "divice")
//@NotNull(message="Divice field is mandatory.")
private int divice;
@Getter
@Setter
@Column(name = "operating_system")
//@NotNull(message="Operating system field is mandatory.")
private int operating_system;
@Getter
@Setter
@Column(name = "google_analytics_id")
@NotNull(message="Google analytics Id field is mandatory.")
private String google_analytics_id;
@Getter
@Setter
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "publisher_id")
private PublisherEntity publishers;
@Getter
@Setter
@ManyToOne
@JoinColumn(name = "agency_id")
private AgencyEntity agencies;
@Override
public String toString() {
return "CampaignEntity [campaign_id=" + campaign_id + ", name=" + name
+ ", no_of_views=" + no_of_views + ", video_url=" + video_url
+ ", start_date=" + start_date + ", end_date=" + end_date
+ ", click_to_play=" + click_to_play + ", frequency="
+ frequency + ", priority=" + priority + ", divice=" + divice
+ ", operating_system=" + operating_system
+ ", google_analytics_id=" + google_analytics_id
+ ", publishers=" + publishers + ", agencies=" + agencies + "]";
}
}
我的机构实体就像是:
package com.nativeunlock.dto;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import lombok.Getter;
import lombok.Setter;
@SuppressWarnings("serial")
@NamedQueries({
@NamedQuery(
name = AgencyEntity.GET_AGENCIES_QUERY,
query = "from AgencyEntity agnecy"
)
})
@Entity
@Table(name = "agency")
public class AgencyEntity implements Serializable {
public static final String GET_AGENCIES_QUERY ="getAgencyList";
@Id
@Getter
@Setter
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "agency_id", unique = true, nullable = false)
private int agency_id;
@Getter
@Setter
@Column(name = "name")
private String name;
@OneToMany (mappedBy="agencies", cascade={CascadeType.PERSIST, CascadeType.REMOVE})
@org.hibernate.annotations.Cascade( {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private Set<CampaignEntity> campaigns = new HashSet<CampaignEntity>();
}
以下是插入记录的代码。
@Override
public boolean saveCampaign(CampaignEntity campaignObj) {
try{
Session session=getCurrentSession();
session.beginTransaction();
session.saveOrUpdate(campaignObj);
session.getTransaction().commit();
return true;
} catch(Exception e) {
e.printStackTrace();
}
return false;
}
感谢您的预先帮助。