Deivid Santos Alves
Deivid Santos Alves

Reputation: 25

Hibernate insert error

Error message:

javax.persistence.RollbackException: Error while committing the transaction

I'm having trouble saving user data. This error has appeared, it follows the form that the project is in:

Method to save:

   public void save(User u)
   { 
       EntityManager em = JPAUtil.getEntityManager();
       EntityTransaction tx = em.getTransaction();        

        try 
        {
        tx.begin();
        if (u.getId_User()== null) {

            em.persist(u);
        } else {
            em.merge(u);
        }
        tx.commit();
    } catch (Exception ex) {
        ex.printStackTrace();
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
    } finally {
        em.close();
    }
}

Method that receives the form data:

 public String addUser(Address a, User u)  
 {
    u.setAddress(a);
    userDAO.save(u);
    return "User saved successfully!";
}

Persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="consultoriowebPU">
    <!-- Hibernate -->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/bd_consultorioweb?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8"/> 
        <property name="javax.persistence.jdbc.user" value="root"/> 
        <property name="javax.persistence.jdbc.password" value=""/> 
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />            
    </properties>
</persistence-unit>

User.class [model]:

 @Entity
 public class User implements Serializable 
 {
    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue
    private Long Id_User;
    private String email_User;
    private String name_User;
    private String password_User;
    private int status_User;
    @ManyToOne
    private Address address;
    //Get and set..
  }

Address.class [model]:

@Entity
public class Address implements Serializable
{
   private static final long serialVersionUID = 1L;

   @Id @GeneratedValue
   private Long Id_Address;
   private String Country;
   private String State;
   private String City;
   private String Neighborhood;
   private String Street;
   private Long Number;
   private String Complement;
   private String Zipcode;

   //Get and set..    
 }

Here is the list of libraries/jar used: https://i.sstatic.net/09n2v.jpg In the console, only the insert sql appears, and then the error message and no data is recorded in the database.

Upvotes: 2

Views: 470

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26522

I assume that the Address entity that you are populating the user with, is a detached entity.

I would add the following cascade options to the User entity:

@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "address_id")
private Address address;

Now when the User entity is persisted / merged, then also the Address entity will be persisted / merged.

Upvotes: 1

Related Questions