2
JPA.em("default").createQuery("insert into USER (FULLNAME, EMAIL, USERNAME, PASSWORD) " + " VALUES (\'"+fullname+"\',\'"+email+"\',\'"+username+"\',\'"+password+"\');");

is it a wrong query? i get this error:

[IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: VALUES near line 1, column 57 [insert into USER (FULLNAME, EMAIL, USERNAME, PASSWORD) VALUES ('Doniyor','ikbola-86@bk.ru','jurabayev','er');]]

i dont know why this is happening, the query string is actually okay, right?

would appreciate any help!

thanks

doniyor
  • 36,596
  • 57
  • 175
  • 260

3 Answers3

17

i think you should use

.createNativeQuery(...);

instead of

.createQuery(...);

but I'm not sure about it.

if you are using annotations

@Query(value = "your_query_here", nativeQuery = true)

duffy356
  • 3,678
  • 3
  • 32
  • 47
  • thank you, that did the job, but it didnot save any data into table :( – doniyor Jun 20 '12 at 17:45
  • did you call the .executeUpdate() method of your query object?? – duffy356 Jun 20 '12 at 18:57
  • 2
    the Method .createNativeQuery(...) creates your query for the database, but it doesn't execute it. To execute your query you have to call the method .executeUpdate() on your query object. which would be now .createNativeQuery(...).executeUpdate(); maybe this post could help you: http://stackoverflow.com/questions/5157051/hibernate-native-query-insert-wont-work-for-java-util-date-someone-please-help – duffy356 Jun 20 '12 at 19:27
  • whoever you are, you saved me!!! thaaaaaaaaanks a lot! :D. that worked. i didnot use 1) executeUpdate() and 2) setParameter was very important. thanks thanks thanks – doniyor Jun 20 '12 at 19:41
  • Helped in Hibernate too hahaha – Adelin Nov 21 '15 at 20:14
1

It looks as though you're trying to use an SQL statement for the query instead of JPAQL, which I don't believe has an Insert Into anyways, although I'm still new to the Play Framework myself so I may be mistaken.

I believe what you should be doing is creating the user you wish to insert, and then persist that object, since you are using JPA.

So your code would look something along these lines:

User u = new User('Doniyor','ikbola-86@bk.ru','jurabayev','er');
JPA.em("default").persist(u);
Jeff LaJoie
  • 1,725
  • 2
  • 17
  • 27
  • thanks, not it says, [IllegalArgumentException: Unknown entity: models.User]. but User is there. – doniyor Jun 20 '12 at 17:48
  • You need to have User annotated with JPA as an entity, using @Entity. A good example of this can be found on the Play! website: http://www.playframework.org/documentation/2.0.1/JavaEbean I'm sure with a bit of Googling you'll be able to find what how to do the annotations for your User class. – Jeff LaJoie Jun 20 '12 at 17:52
  • yeah, but if i put this entity, is gives me this error: **PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to configure EntityManagerFactory** this is killing me – doniyor Jun 20 '12 at 18:16
0

This will also do.......

String hql = String.format("insert into attendence(slip_change_cntr,roll_no)values(0,"+a+")");
    Query query = sessionHb.createSQLQuery(hql);
    return query.executeUpdate();
Rohhit
  • 722
  • 3
  • 12
  • 25