0

I could not understand what I'm doing wrong

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'USER_ID' cannot be null

User and Document Id are auto increment ... @GeneratedValue(strategy = GenerationType.IDENTITY)

public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    ..
}

... OR ...

public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ..
}

Don't work

@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class DocumentService implements Serializable {
    @Inject
    @LoggedIn
    private User currentUser;


    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public Document add(Document document) throws BusinessException {
        if (document.isTransient()) {
            System.out.println("dao " + currentUser); // print user correct
            document.setUser(currentUser);
        }
        System.out.println("dao " + d.getUser()); // print user correct
        return dao.save(document);
    }
}

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class DAO<T extends BaseEntity> implements Serializable {

    private static final long serialVersionUID = 1L;
    private final Class<T> entityClass;
    private EntityManager entityManager;

    public DAO(Class<T> entityClass, EntityManager entityManager) {
        this.entityClass = entityClass;
        this.entityManager = entityManager;

    }

    @TransactionAttribute(TransactionAttributeType.MANDATORY)
    public T save(T entity) {

        Document d = (Document) entity;
        System.out.println("dao " + d.getUser()); // print user correct
        System.out.println("contains " + entityManager.contains(entity)); // return false
        if (entity.isNew()) {
            entityManager.persist(entity); // persist error USER_ID is null
            entityManager.flush();
        } else {
            entityManager.merge(entity);
        }

        return entity;
    }
}


public class Document {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    @NotNull
    @ManyToOne
    private User user;
}
the relationship is unidirectional Document for User

stack.. 20:30:02,917 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-6) SQL Error: 1048, SQLState: 23000 20:30:02,917 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-6) Column 'USER_ID' cannot be null 20:30:02,930 ERROR [org.jboss.as.ejb3.invocation] (default task-6) JBAS014134: EJB Invocation failed on component DocumentService for method public com.taskboard.model.Document com.taskboard.boundary.DocumentService.save(com.taskboard.model.Document) throws com.taskboard.core.exception.BusinessException: javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement

ramirescm
  • 1
  • 1
  • 3
  • Add `Document` class to the question. Do you have `(cascade=CascadeType.PERSIST)` on the Document-User relationship? – Gas Oct 28 '14 at 23:15
  • I didnt see in your code where you set the User to the document. – fmodos Oct 28 '14 at 23:17
  • the relationship is unidirectional Document for User and i haven't (cascade=CascadeType.PERSIST) on the Document-User relationship – ramirescm Oct 29 '14 at 00:09
  • Is the `User` already in the DB? You must save the `User` before saving the `Document`. It seems you're not. – rdcrng Oct 29 '14 at 07:46
  • Yes, the user is registered in the database. Otherwise it can not login if you have a previously registered user. The user logs in, then add the user in the session, I have a producer that takes the user id of the logged in user and returns the user to the database, this user is to inject the service to add to the document. @rdcrng – ramirescm Nov 03 '14 at 21:36
  • Have you enabled the log to see the created query and the parameters sent? Make a test loading the user from the database before the persist. – uaiHebert Nov 06 '14 at 20:27
  • If I search the user before saving the document and add the document is saved. but the error is thrown JBAS014134: EJB Invocation failed on component DocumentService for method public com.taskboard.model.Document com.taskboard.boundary.DocumentService.findById(java.lang.Long): javax.ejb.EJBException: java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [com.taskboard.model.Document (n/a)] .. I'm trying to print the parameters @uaiHebert – ramirescm Nov 06 '14 at 22:18
  • Check the passed value is a Long, the argument is wrong. Maybe an int or String or Null – uaiHebert Nov 06 '14 at 22:46
  • print the parameters and the User is null at the time of insert , I could not understand why uaiHebert – ramirescm Nov 08 '14 at 00:26
  • If i seek the user and add in the document and persist, the document is saved correctly. I am reviewing my producer. uaiHebert – ramirescm Nov 08 '14 at 00:41
  • very thanks guys!! problem fixed Gas, rdcrng, uaiHebert – ramirescm Nov 08 '14 at 00:58

0 Answers0