java - JPA query using domain object Spring mvc jpa rest -
i want know if possible build jpa query using domain object.
for example:
@entity public class user { private string firstname; private string lastname; private string email; }
in real object has more fields. receive valid json string of object. not fields filled.
for example filled firstname:
{"firstname":"peter","lastname":"","email":""}
this json gets deserialized user object
and want make search using object received parameter. result should user firstname peter.
the question is possible give object query?
thanks inputs
edit 1
thanks until now. found solution search contracts using entity. in solution post updated values of contract , other methods.
the json this:
contract = {fromdate:moment($('#datepickervon').val(), 'dd-mm-yyyy').format('dd-mm-yyyy'), enddate:moment($('#datepickervon').val(), 'dd-mm-yyyy').format('dd-mm-yyyy'), season: { season: "so14"}, name: {name: "peter"}, category:{category:"somestring"}}; console.log(contract); $.ajax({ url:'/contracts/search/', datatype: "json", type: "post", mimetype: 'application/json', contenttype: "application/json", data: json.stringify(contract), success: function(data) { console.log(data); } });
the controller receives this:
@requestmapping(value = "/search/", method = requestmethod.post, headers = "accept=application/json") public responseentity<string> getvertagfromsearch(@requestbody string json, uricomponentsbuilder uribuilder){ contract contract = contract.fromjsontocontract(json); list<contract> contractlist = contract.findallcontractspercontract(contract); return new responseentity<string>(contract.tojsonarray(contractlist), headers, httpstatus.ok); }
the deserialization here:
public static contract fromjsontocontract(string json) { return new jsondeserializer<contract>().use(calendar.class, new calendartransformer("dd-mm-yyyy hh:mm")).use(null, contract.class).deserialize(json); }
and here transaction method.
@transactional public static list<contract> findallcontractspercontract(contract contract) { entitymanager e = entitymanager().getentitymanagerfactory().createentitymanager(); session session = e.unwrap(session.class); example contractexample = example.create(contract); criteria c = session.createcriteria(contract.class).add(contractexample); list<contract> list = c.list(); return list; }
with example results. question now, possible add criteria sql "bigger or less" or "like". because @ moment seems criteria searched "equals".
thanks
yes wouldnt result. happen jpa provider (in case hibernate) detect type entity object , comparison based on @id of entity supplying, guess in case have null id.
you have like:
@entity @namedquery(name="user.findbyfirstname", query="select u user u u.firstname = :firstname") public class user { private string firstname; private string lastname; private string email; }
and on session bean
public class userbean{ public list<user> findusers(user user){ return getentitymanager().createnamedquery("user.findbyfirstname") .setparameter("firstname", user.getfirstname()) .getresultlist(); } }
Comments
Post a Comment