10

I want to insert data into a table using the following code

    public User registerUser(String usr, String pwd) {

    u=em.find(User.class,usr);
    if(u!=null)
    {
        return null;
    }
    String query1 = "insert into users values('" + usr + "','" + pwd +"')";
    Query q = em.createQuery(query1);
    u=em.find(User.class,usr);
    return u;

}

here 'u' is the object of User class and em is EntityManager.

I get this following exception:

Servlet.service() for servlet action threw exception org.hibernate.hql.ast.QuerySyntaxException: expecting OPEN, found 'values' near line 1, column 19 [insert into users values('pawan','am')]

0

3 Answers 3

16

Try

public User registerUser(String usr, String pwd) {

    u=em.find(User.class,usr);
    if(u!=null)
    {
        return null;
    }

    //Now saving...
    em.getTransaction().begin();
    em.persist(u); //em.merge(u); for updates
    em.getTransaction().commit();
    em.close();

    return u;
}

If the PK is Identity, it will be set automatically in your persisted class, if you are using auto generation strategy (thanks to David Victor).

Edit to @aman_novice comment: set it in your class

//Do this BEFORE getTransaction/persist/commit
//Set names are just a example, change it to your class setters
u.setUsr(usr);
u.setPwd(pwd);

//Now you can persist or merge it, as i said in the first example
em.getTransaction().begin();
(...)

About @David Victor, sorry I forgot about that.

Sign up to request clarification or add additional context in comments.

2 Comments

how will this insert pwd in the User table, along with usr??
The PK will only be set depending on the generation strategy. i.e. an @GeneratedValue(strategy = GenerationType.AUTO) annotation or similar. It is also better to rely on declarative txn boundaries generally.
5

You're not using SQL but JPAQL, there is no field-based insert. You persist object rather than inserting rows.

You should do something like this:

public User registerUser(String usr, String pwd) {
    u=em.find(User.class,usr);
    if(u!=null)
    {
        return u;
    }
    u = new User(usr, pwd);
    em.persist(u);
    return u;
}

4 Comments

ya true...my bad..wrote it as SQL. however, i would like to point out that User() is a no-parameter constructor. So i cannot create that object!!! :(
@aman_novice: well, then use the setter methods to set the username and password.
I think there will be a problem here with the type of the identifier for the User class. Need to ensure the id type is String on the entity. Which would work - but is unusual. :-)
thanks i was looking for something along these lines ! +1 :-)
0

This isn't really the way to go. You are trying to insert a row in a table but have no associated attached entity. If you're using the JPA entity manager - then create a new instance - set the properties & persist the entity.

E.g.

User u = new User();
u.setXXX(xx);

em.persist(u);
// em.flush()   <<-- Not required, useful for seeing what is happening

// etc..

If you enable SQL loggging in Hibernate & flush the entity then you'll see what is sent to the database.

E.g. in persistence.xml:

    <property name="hibernate.format_sql" value="true" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.