- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 JavaEE 新手,并且在使用实体类和 ejb 实体管理器持久保存一个表时遇到问题,该表的外键指向另一个表的主键。我正在使用 Netbeans。
我有一个名为“property”的实体,还有另一个名为“offer”的实体,它保存指向该属性主键的外键。基本上,逻辑是一处特性可以有多个报价。因此,我尝试使用实体管理器在“报价”表中添加新报价,但我无法做到这一点。您可以查看代码,看看我可能缺少什么。
属性实体:
@Entity
@Table(name = "PROPERTY")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Property.findAll", query = "SELECT p FROM Property p")})
public class Property implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "PROPERTYID")
private Integer propertyid;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 500)
@Column(name = "DESCRIPTION")
private String description;
@Size(max = 50)
@Column(name = "TYPE")
private String type;
@Column(name = "NUMBEROFBEDROOM")
private Integer numberofbedroom;
@Column(name = "NUMBEROFBATHROOM")
private Integer numberofbathroom;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 10)
@Column(name = "ISFURNISHED")
private String isfurnished;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 10)
@Column(name = "HASGARDEN")
private String hasgarden;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "SIZE")
private String size;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "PRICE")
private String price;
@Basic(optional = false)
@NotNull
@Column(name = "ENTEREDDATE")
@Temporal(TemporalType.DATE)
private Date entereddate;
@Basic(optional = false)
@NotNull
@Column(name = "ENTEREDTIME")
@Temporal(TemporalType.TIME)
private Date enteredtime;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "property")
private Paddress paddress;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "propertyid")
private Collection<Offer> offerCollection;
@JoinColumn(name = "PROPERTYOWNERID", referencedColumnName = "PROPERTYOWNERID")
@ManyToOne(optional = false)
private Propertyowner propertyownerid;
@JoinColumn(name = "REALESTATEAGENTID", referencedColumnName = "REALESTATEAGENTID")
@ManyToOne(optional = false)
private Realestateagent realestateagentid;
public Property() {
}
public Property(Integer propertyid) {
this.propertyid = propertyid;
}
public Property(Integer propertyid, String description, String isfurnished, String hasgarden, String size, String price, Date entereddate, Date enteredtime) {
this.propertyid = propertyid;
this.description = description;
this.isfurnished = isfurnished;
this.hasgarden = hasgarden;
this.size = size;
this.price = price;
this.entereddate = entereddate;
this.enteredtime = enteredtime;
}
public Integer getPropertyid() {
return propertyid;
}
public void setPropertyid(Integer propertyid) {
this.propertyid = propertyid;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getNumberofbedroom() {
return numberofbedroom;
}
public void setNumberofbedroom(Integer numberofbedroom) {
this.numberofbedroom = numberofbedroom;
}
public Integer getNumberofbathroom() {
return numberofbathroom;
}
public void setNumberofbathroom(Integer numberofbathroom) {
this.numberofbathroom = numberofbathroom;
}
public String getIsfurnished() {
return isfurnished;
}
public void setIsfurnished(String isfurnished) {
this.isfurnished = isfurnished;
}
public String getHasgarden() {
return hasgarden;
}
public void setHasgarden(String hasgarden) {
this.hasgarden = hasgarden;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public Date getEntereddate() {
return entereddate;
}
public void setEntereddate(Date entereddate) {
this.entereddate = entereddate;
}
public Date getEnteredtime() {
return enteredtime;
}
public void setEnteredtime(Date enteredtime) {
this.enteredtime = enteredtime;
}
public Paddress getPaddress() {
return paddress;
}
public void setPaddress(Paddress paddress) {
this.paddress = paddress;
}
@XmlTransient
public Collection<Offer> getOfferCollection() {
return offerCollection;
}
public void setOfferCollection(Collection<Offer> offerCollection) {
this.offerCollection = offerCollection;
}
public Propertyowner getPropertyownerid() {
return propertyownerid;
}
public void setPropertyownerid(Propertyowner propertyownerid) {
this.propertyownerid = propertyownerid;
}
public Realestateagent getRealestateagentid() {
return realestateagentid;
}
public void setRealestateagentid(Realestateagent realestateagentid) {
this.realestateagentid = realestateagentid;
}
public String dateToString()
{
DateFormat df = new SimpleDateFormat("dd/MM/yy");
return df.format(entereddate);
}
public String timeToString()
{
DateFormat df = new SimpleDateFormat("HH:mm:ss");
return df.format(enteredtime);
}
@Override
public int hashCode() {
int hash = 0;
hash += (propertyid != null ? propertyid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Property)) {
return false;
}
Property other = (Property) object;
if ((this.propertyid == null && other.propertyid != null) || (this.propertyid != null && !this.propertyid.equals(other.propertyid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.sushan.model.Property[ propertyid=" + propertyid + " ]";
}
}
EJB PropertyDAO:
@Stateless
public class PropertyDAO implements PropertyDAOLocal {
@PersistenceContext(unitName="RealEstateWebsite-ejbPU")
private EntityManager em;
@Override
public void AddProperty(Property property) {
em.persist(property);
}
@Override
public void EditProperty(Property property) {
em.merge(property);
}
@Override
public void DeleteProperty(int propertyId) {
em.remove(GetProperty(propertyId));
}
@Override
public List<Property> GetAllProperty() {
return em.createNamedQuery("Property.findAll").getResultList();
}
@Override
public Property GetProperty(int propertyId) {
return em.find(Property.class, propertyId);
}
@Override
public void EditPropertyAddress(Paddress propertyAddress) {
em.merge(propertyAddress);
}
}
要约实体:
@Entity
@Table(name = "OFFER")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Offer.findAll", query = "SELECT o FROM Offer o")})
public class Offer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "OFFERID")
private Integer offerid;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "OFFERSTATUS")
private String offerstatus;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "ORIGINALOFFER")
private String originaloffer;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "OFFERINGBP")
private String offeringbp;
@Basic(optional = false)
@NotNull
@Column(name = "OFFEREDDATE")
@Temporal(TemporalType.DATE)
private Date offereddate;
@Basic(optional = false)
@NotNull
@Column(name = "OFFEREDTIME")
@Temporal(TemporalType.TIME)
private Date offeredtime;
@JoinColumn(name = "BUYERID", referencedColumnName = "BUYERID")
@ManyToOne(optional = false)
private Buyer buyerid;
@JoinColumn(name = "PROPERTYID", referencedColumnName = "PROPERTYID")
@ManyToOne(optional = false)
private Property propertyid;
public Offer() {
}
public Offer(Integer offerid) {
this.offerid = offerid;
}
public Offer(String offerstatus, String originaloffer, String offeringbp, Date offereddate, Date offeredtime, Buyer buyerid, Property propertyid) {
this.offerstatus = offerstatus;
this.originaloffer = originaloffer;
this.offeringbp = offeringbp;
this.offereddate = offereddate;
this.offeredtime = offeredtime;
this.buyerid = buyerid;
this.propertyid = propertyid;
}
public Integer getOfferid() {
return offerid;
}
public void setOfferid(Integer offerid) {
this.offerid = offerid;
}
public String getOfferstatus() {
return offerstatus;
}
public void setOfferstatus(String offerstatus) {
this.offerstatus = offerstatus;
}
public String getOriginaloffer() {
return originaloffer;
}
public void setOriginaloffer(String originaloffer) {
this.originaloffer = originaloffer;
}
public String getOfferingbp() {
return offeringbp;
}
public void setOfferingbp(String offeringbp) {
this.offeringbp = offeringbp;
}
public Date getOffereddate() {
return offereddate;
}
public void setOffereddate(Date offereddate) {
this.offereddate = offereddate;
}
public Date getOfferedtime() {
return offeredtime;
}
public void setOfferedtime(Date offeredtime) {
this.offeredtime = offeredtime;
}
public Buyer getBuyerid() {
return buyerid;
}
public void setBuyerid(Buyer buyerid) {
this.buyerid = buyerid;
}
public Property getPropertyid() {
return propertyid;
}
public void setPropertyid(Property propertyid) {
this.propertyid = propertyid;
}
@Override
public int hashCode() {
int hash = 0;
hash += (offerid != null ? offerid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Offer)) {
return false;
}
Offer other = (Offer) object;
if ((this.offerid == null && other.offerid != null) || (this.offerid != null && !this.offerid.equals(other.offerid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.sushan.model.Offer[ offerid=" + offerid + " ]";
}
EJB OfferDAO:
@Stateless
public class OfferDAO implements OfferDAOLocal {
@PersistenceContext(unitName="RealEstateWebsite-ejbPU")
EntityManager em;
@Override
public void EditOffer(Offer offer) {
em.merge(offer);
}
@Override
public List<Offer> GetAllOffer(int propertyId) {
return em.createNamedQuery("Offer.findByPropertyID").setParameter("propertyID", propertyId).getResultList();
}
@Override
public List<Offer> GetAllOffer() {
return em.createNamedQuery("Offer.findAll").getResultList();
}
@Override
public void Add(Offer offer) {
em.persist(offer);
}
}
连接 JSP 和 EJB 的 Servlet:
String action = request.getParameter("action");
String currencyType = request.getParameter("ddlCurrencyType");
String amount = request.getParameter("offerAmount");
String propertyIdStr = request.getParameter("hdnbt");
if ("Offer".equalsIgnoreCase(action))
{
if ("".equals(action) & !"".equals(currencyType) & !"".equals(amount) & !"".equals(propertyIdStr))
{
DateFormat df = new SimpleDateFormat("dd/MM/yy");
DateFormat df1 = new SimpleDateFormat("HH:mm:ss");
Date currentDate = new Date();
Date currentTime = new Date();
int propertyId = Integer.parseInt(propertyIdStr);
try {
currentDate = df.parse(df.format(currentDate));
currentTime = df1.parse(df1.format(currentTime));
} catch (ParseException e) {
}
Buyer buyer = buyerDAO.GetBuyer(1);
Property property = propertyDAO.GetProperty(propertyId);
Offer offer = new Offer("Pending", amount, amount, currentDate, currentTime, buyer, property);
offerDAO.Add(offer);
}
else
{
}
}
request.setAttribute("allProperty", propertyDAO.GetAllProperty());
request.getRequestDispatcher("AdministerProperty.jsp").forward(request, response);
我在这里遗漏了什么吗?我遵循了一个没有外键指导的教程,但尝试使用我自己的逻辑来绕过它,但它不起作用。我找不到使用与我类似的方法的可靠来源。我找到的大多数资源都是针对 Hibernate 的,但我使用的是 EJB。
最佳答案
似乎检索属性的方法和保存 Offer 的方法在单独的事务中运行(DAO 是无状态 session bean)。
这意味着 Offer 是使用独立的属性来填充和持久化的,因此持久化提供者不会意识到这一点。
不确定为什么没有引发异常,但您必须先合并属性,或者在保留要约时在同一事务中执行查询:
@Override
public void Add(Offer offer, int peropertyId) {
Property property = em.find(Property.class, propertyId);
offer.setPeropertyId(property);
em.persist(offer);
}
或
@Override
public void Add(Offer offer, Property peroperty) {
Property managedProperty = em.merge(property);
offer.setPeropertyId(managedProperty);
em.persist(offer);
}
关于java - 使用 EJB 和 JPA 保留带有外键的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42748682/
我试图弄清楚如何为聊天气泡制作外 Angular 圆形设计,以获得所需的结果: 我必须使用气泡作为不同背景的组件,没有相同和纯色,但有一些设计元素,所以气泡周围的空间必须是透明的: 我试过将元素添加为
我尝试了 display:table-cell 但它没有用。我怎样才能在div中显示这个词。现在它显示溢出了 div。我在我的网页上使用 CSS2。提前致谢。 Visit W3Schools
我有一个使用 CSS 隐藏在 View (对于移动设备)之外的菜单: #filter-column { position:absolute; left:-400px; } 当用户单击链
我想创建一个这样的问题行 http://imageshack.us/photo/my-images/200/questionh.png/ 此时我的html源是: question label
我要mock a class with Ruby . 如何编写处理样板代码的方法? 以下代码: module Mailgun end module Acani def self.mock_mail
请不要担心循环,但我的问题是关于这些关键字:outer、middle 和 inner。它们不是声明为实例变量,为什么IDE让代码编译?我在谷歌上搜索了一下,这是java标签吗? Java中的某种关键字
我有一个数据框(df),看起来像, Id Name Activity. 1 ABC a;sldkj kkkdk 2 two
Elasticsearch内存中有哪些东西可以使搜索如此快速? 是所有json本身都在内存中,还是仅倒排索引和映射将在内存中24 * 7? 最佳答案 这是一个很好的问题,然后简而言之就是: 不仅仅是数
我正在尝试添加用户在用户界面上选择的值。对于数据库中的特定列,我已经与数据库建立了连接,当我按“保存”时,新的 id 会添加到数据库中,控制台中不会显示任何错误,但我要提交的值不会放入数据库,我怎样才
我不确定这个问题是否应该涉及电子领域,但由于它是关于编程的,所以我在这里问了它。 我正在制作一个数字时钟,使用由移位寄存器供电的 LED,而不是 7 段显示器。无论如何,当使用 CCS 编译代码时,我
我希望用户在 div 中选择文本 (html)。然而,这样做会在浏览器中显示选择背景,也在 div 之外。 我可以用(参见 http://jsfiddle.net/lborgman/aWbgT/)来防
我有以下 Razor View @{ ViewBag.Title = "UserCost"; }
我使用 KineticJS 和 D3.js 制作了以下内容。当用户将鼠标悬停在其中一个点上时,我使用 KineticJS 让我弹出工具提示。但是,由于 Canvas 的边界,工具提示似乎被切断了。有没
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this qu
我正在使用 primefaces 学习 Java Web 和 jsf。 我的项目当前只有一个index.xhtml 文件,当我访问localhost:8080/appname/时,index.xhtm
我是 ios 新手。 我有一个 View ,其中我使用 Quarts 核心绘制了一个圆圈。 我在该圆圈中放置了一个 UIButton,并赋予了拖放该按钮的功能。 现在我想要限制按钮不能被拖出那个圆圈区
这个问题已经有答案了: How to add two strings as if they were numbers? [duplicate] (20 个回答) How to force JS to
我正在创建简单的文本从右侧滑动到页面的 css 动画。我正在使用 jQuery 通过向元素添加一个类来触发动画。但是起始位置必须在视口(viewport)之外,这会触发底部滚动条出现。如何预防? 这是
我编写了一个简单的代码来评估一段代码并将输出写入文件。这样它减少了我的一些,因为我需要很多很多文件,每一行都包含返回值! 无论如何,我正在使用的代码是: #!/usr/bin/ruby -w def
所以我试图在我的一款游戏中加入一个非常基本的“手电筒”式的东西。 我让它工作的方式是在我的游戏屏幕顶部有一个层,这个层会绘制一个黑色矩形,不透明度约为 80%,在我的游戏场景顶部创建黑暗的外观。 cc
我是一名优秀的程序员,十分优秀!