我正在使用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;
}
提前致谢
答案 0 :(得分:0)
您必须在@Cascade
CampaignEntity
字段
agencies
使用@Getter
@Setter
@ManyToOne
@JoinColumn(name = "agency_id")
@org.hibernate.annotations.Cascade( {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private AgencyEntity agencies;
注释
{{1}}
答案 1 :(得分:0)
尝试用session.beginTransaction()
替换session.getTransaction()
,然后提交该事务。如果Spring管理您的事务,那么事务可能已经存在,您不需要自己启动另一个事务。
@Override
public boolean saveCampaign(CampaignEntity campaignObj) {
try{
Session session = getCurrentSession();
session.getTransaction().begin();
session.saveOrUpdate(campaignObj);
session.getTransaction().commit();
return true;
} catch(Exception e) {
e.printStackTrace();
}
return false;
}