Posts

Showing posts with the label JPA

Digital Marketing

How to solve: JPA eclipselink: during the execution of the query was detected to be null. Primary keys must not contain null

EclipseLink is case sensitive by default unless the eclipselink.jpa.uppercase-column-names persistent property is set to true. This might be the problem for native queries if the database returns the column name as upper-case "ID" when you have it defined as lower-case "id". Try changing the column definitions in the annotations to match what your database uses or adding the property with a value of true. In persistence.xml: <properties> <property name= "eclipselink.target-server" value= "SunAS9" /> <property name= "eclipselink.logging.level" value= "ALL" /> <property name= "eclipselink.logging.level.sql" value= "ALL" /> <property name= "eclipselink.logging.parameters" value= "true" /> <property name= "eclipselink.jdbc.batch-writing" value= "JDBC" /> <property name= "eclipselink.tar...

Operator Difference: "MEMBER OF" vs "IN" in JPA

JPA distinguishes between the MEMBER OF operator, which should be used for checking a collection field , and the IN operator, which should be used for checking other collections, such as a collection that is passed to the query as a parameter. For example: 'fido' MEMBER OF person.dogs 'fido' IN :dogs

JPA Join example using Criteria

CriteriaBuilder cb = em .getCriteriaBuilder(); CriteriaQuery cq = cb .createQuery(Email.class); Root root = cq .from(Email.class); Join accountJoin = root. join (Email_.account); Predicate predicate = cb .isMember(user, accountJoin. get (Account_.users))); cq .where(predicate);

How to get EntityManagerFactory in Java SE

EntityManagerFactory emf = Persistence.createEntityManagerFactory( "I88CA_MYSQLPU" );

How to fix: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'

If you are using JPA and get the exception: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist' But you are not clear what exactly goes wrong. You can fix it by adding the code to show you more detail about the validation exception:

EclipseLink and JPQL

Functions JPQL supports several database functions. These functions are database independent in name and syntax, but require database support. If the database supports an equivalent function or different syntax the standard JPQL function is supported, if the database does not provide any way to perform the function, then it is not supported. For mathematical functions (+, -, /, *) BEDMAS rules apply. In EclipseLink's JPQL support functions can be used in the SELECT, WHERE, ORDER BY, GROUP BY and HAVING clauses, as well as inside other functions, with comparison operators, and in constructors. EclipseLink provides support for several functions beyond the JPA spec. EclipseLink also supports calling specific database functions through FUNCTION and OPERATOR. JPQL supported functions Function Description Example - subtraction e.salary - 1000 + addition e.salary + 1000 * multiplication e.salary * 2 / division e.salary / 2 ABS absolute value ABS ( e.salary - e.manager.salary ) CASE define...

How to bind an instance of java.util.Date/Calendar to a named parameter in JPA

 Query setParameter (String name, Calendar value, TemporalType temporalType)           Bind an instance of java.util.Calendar to a named parameter.  Query setParameter (String name, Date value, TemporalType temporalType)           Bind an instance of java.util.Date to a named parameter.  Query setParameter (int position, Calendar value, TemporalType temporalType)           Bind an instance of java.util.Calendar to a positional parameter.  Query setParameter (int position, Date value, TemporalType temporalType)           Bind an instance of java.util.Date to a positional parameter.

Create new Object In JPQL Select Statement by Constructor Expression

When you want to retrieve a Data Transfer Object instead of retrieving a full entity or a full graph of objects in a type safe way, then you can create the new Object in the JPQL select statement: SELECT NEW ca . i88 . it . UserModel ( u . name , u . phone ) FROM User u; Here a constructor is used in the SELECT list to return one or more Java instances. The constructor name must be fully qualified.

Simple Java example to call a stored procedure in JPA / JDBC

Example 1:  public void myMethod( Lists list, Account account) { Query query = em . createNativeQuery( " call example_procedure(?,?); " ); query . setParameter( 1 , list . getListid()) . setParameter( 2 , account . getUserid()); query . executeUpdate(); } or public void myMethod( Lists list, Account account) { em . createNativeQuery( " call example_procedure(?p1,?p2); " ) .setParameter( " p1 " , list . getListid()) .setParameter( " p2 " , account . getUserid()) . executeUpdate(); } Example 2: // for getting the result back. String q = " call `i88ca`.`example_procedure2`(); " ; Object o = em . createNativeQuery(q) . getSingleResult(); // or List l = em . createNativeQuery(q) . getResultList(); PROCEDURE `example_procedure2`() BEGIN select 8888 ; -- or any other select statements for output. END Example 3: @Resource(name = " db2 " ) private DataSource dataSource; public vo...

Simple Java example to call a stored procedure in JPA / JDBC

Example 1:  public void myMethod( Lists list, Account account) { Query query = em . createNativeQuery( " call example_procedure(?,?); " ); query . setParameter( 1 , list . getListid()) . setParameter( 2 , account . getUserid()); query . executeUpdate(); } or public void myMethod( Lists list, Account account) { em . createNativeQuery( " call example_procedure(?p1,?p2); " ) .setParameter( " p1 " , list . getListid()) .setParameter( " p2 " , account . getUserid()) . executeUpdate(); } Example 2: // for getting the result back. String q = " call `i88ca`.`example_procedure2`(); " ; Object o = em . createNativeQuery(q) . getSingleResult(); // or List l = em . createNativeQuery(q) . getResultList(); PROCEDURE `example_procedure2`() BEGIN select 8888 ; -- or any other select statements for output. END Example 3: @Resource(name = " db2 " ) private DataSource dataSource; public vo...