0

I am trying to make a simple insert into a DB with HQlL by using native SQL code.

It doesn't give any error, it just doesn't work. Any help is appreciated.

Thanks.

public void AddMedicament(Medicament medicament) {
    System.out.println(medicament.getName());
    // open a database connection
    Session session = FarmacieHibernateUtil.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();
    // prepare SQL insert command
    session.createSQLQuery("insert into Medicament(name) values('test')");

    // close the database connection
    session.close();
}

4 Answers 4

2

You need to call

session.executeUpdate() 
transaction.commit(); 

before closing session.

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

1 Comment

also transaction.commit();
2

I am not familar with Hibernate, but i dont see you sre running your command. You just create query and close session I think you need some statement to execute it.

Comments

2

If you use createSQLQuery this throw a native sql instruction

Your object table name is Medicament too?

Comments

2

session.saveOrUpdate(medicament); tx.commit(); then it will insert if u r not setting the Primarykey, if u r setting the PK in the domain object then it will be updated.

no need to executeQuery in hibernate if you are using the Spring ORM.

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.