使用hibernate和vraptor通过POST插入带有外键的实体
Im trying to insert a json taxistaPonto in database using Ajax with JQuery via POST, with other entity without foreign key was fine, but i don't know how to insert this entity with foreign keys. Im sending the requests in Json format. Maybe my json isnt in right format? How can i insert this entity in my database?
我试图在数据库中使用Ajax和JQuery通过POST插入一个json taxistaPonto,其他没有外键的实体很好,但我不知道如何使用外键插入此实体。我以Json格式发送请求。也许我的json不是正确的格式?如何在我的数据库中插入此实体?
Model:
@Entity(name="taxistaPonto")
public class TaxistaPonto {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne
@JoinColumn(name = "fk_taxista",nullable=false)
private Taxista taxista;
@OneToOne
@JoinColumn(name = "fk_ponto",nullable=false)
private Ponto ponto;
public TaxistaPonto() {
super();
}
public TaxistaPonto(Integer id, Taxista taxista, Ponto ponto) {
super();
this.id = id;
this.taxista = taxista;
this.ponto = ponto;
}//... getters and setters
Controller:
@Controller
@Path("/taxistaponto")
public class TaxistaPontoController {
@Inject
private Result result;
@Inject
private TaxistaPontoDAO taxistaPontoDAO;
@Consumes("application/json")
@Post("/add")
public void add(TaxistaPonto taxistaPonto){
taxistaPontoDAO.salvar(taxistaPonto);
}
}
DAO:
public void salvar(TaxistaPonto taxistaPonto) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
try {
session.persist(taxistaPonto);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
}
HibernateUtil.shutdown(session);
}
Here is my request:
这是我的要求:
$(document).ready(function(){
var carro =
{"descricao" : "carro mt legal", "placa" : "12938713"};
var taxista =
{id : 5, "nome" : "12:58", "sexo" : "trans", "login" : "loginmeu", "senha" : "minhasenhsa", carro : carro};;
var ponto =
{id : 8,"latitude" : "40", "longitude" : "96"};
var taxistaPonto =
{taxista : taxista, ponto : ponto};
$.ajax({
type: "POST",
url: "https://expotaxi.herokuapp.com/taxistaponto/add",
data: JSON.stringify(taxistaPonto),
contentType: "application/json",
dataType: "json",
success: function(data){
alert("sucesso");
},
error : function(data){
alert("error");
}
});
});
@EDIT:This is the excpetion:
@EDIT:这是一个重要的事情:
and is shown a error like this too "uncaught exception: out of memory"
并显示像这样的错误“未捕获的异常:内存不足”
1 个解决方案
#1
1
Can you check the taxistaPonto Object on Salver method. Can you share the parsing logic/handling request on server end.
你能在Salver方法上检查taxistaPonto对象吗?你能在服务器端共享解析逻辑/处理请求吗?
For your case, you can rectify this error on server end while processing the add request.
对于您的情况,您可以在处理添加请求时纠正服务器端的此错误。
更多相关文章
- ASP.NET下使用jQuery UI下的Dialog,服务器端按钮无响应的问题。
- HTML H5之ASCII 代码转义字符集实体编号
- 如何用c#获取服务器端元素的innerhtml(内部有另一个服务器端控件)
- 使用没有实体类的本机SQL查询
- PHP获取服务器端的相关信息
- 使用ajax在服务器端恢复json时出错
- php清理当前目录下的指定文件和空目录(源码),建议服务器端执行
- 【动软.Net代码生成器】连接MySQL生成C#的POCO实体类(Model)
- 主义通过扩展来破坏对实体的查询