Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert values into database using queries using entity manager, persistence using a java class?

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')]

like image 303
aman_novice Avatar asked Feb 11 '11 10:02

aman_novice


People also ask

What is the EntityManager method to insert data?

In JPA, we can easily insert data into database through entities. The EntityManager provides persist() method to insert records.

What is persistence in Java with example?

Persistence, in computer science, is a noun describing data that outlives the process that created it. Java persistence could be defined as storing anything to any level of persistence using the Java programming language, but obviously this would be too broad a definition to cover in a single book.

What is persistent entity in Java?

Entities represent persistent data stored in a relational database automatically using container-managed persistence. They are persistent because their data is stored persistently in some form of data storage system, such as a database: they do survive a server failure, failover, or a network failure.

Which method in JPA adds an entity to the persistence context?

The persist method is intended to add a new entity instance to the persistence context, i.e. transitioning an instance from a transient to persistent state. We usually call it when we want to add a record to the database (persist an entity instance): Person person = new Person(); person.


2 Answers

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.

like image 119
Renan Avatar answered Oct 06 '22 20:10

Renan


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;
}
like image 35
Michael Borgwardt Avatar answered Oct 06 '22 19:10

Michael Borgwardt