entitymanager

Is it possible to find all available EntityManager properties?

有些话、适合烂在心里 提交于 2020-01-17 06:04:27
问题 I need to use multiply persistence units with different properties (in my case MySQL and Oracle database). In persistence.xml I define two different "persistence-unit"s and list only the entity classes there. Properties could be set in persitence.xml with <properties> <property name="..." value="..." /> ... Im doing it in a java class before creating the EntityManager, because I must use different properties (which I read before): EntityManagerFactory factory; ... HashMap<String, String>

EntityManager returns NullPointerException when persisting

喜夏-厌秋 提交于 2020-01-15 12:41:22
问题 When I persist a "Joueur" (cf my class below), I get a NullPointerException, but I don't know why em.persist(new Joueur("pseudo", "mdp", "mail")); This is my class "Joueur" : @Entity @NamedQueries({ @NamedQuery(name = "trouverParMail", query = "SELECT * FROM Joueur j WHERE j.mail = :mail")}) public class Joueur implements Serializable { private static final long serialVersionUID = 1L; private String pseudo; private String mdp; private String mail; private Integer nbParties; private Integer

hibernate does not add entity to persistencecontext after persist

青春壹個敷衍的年華 提交于 2020-01-11 06:54:25
问题 I have a managedbean that has a list of entity objects of current page. after I create a new object and persist it to db using persist() in a transaction; in another transaction when I call merge(because the entity is in detached state due to previous transaction commit); entitymanager cannot find the object in persistence context and throw a select query to database. Am I missing something or is that normal behavior? Update: The above problem exists when I use mysql database and

Java /JPA | Query with specified inherited type

余生颓废 提交于 2020-01-09 10:38:07
问题 I am building a query on a generic table "Sample" and I have several types which inherit from this table "SampleOne", "SampleTwo". I require a query like : select s from Sample where s.type = :type where type would be a discriminator value of the table. Is it possible in any way ( and avoid to create an entity specific queries, one for each SampleOne, SampleTwo... etc ) I would greatly appreciate any input in this topic, Kind regards, P. 回答1: In JPA 2.0 you can use TYPE expression (though

What is the easiest way to have CDI and JPA in Java SE?

感情迁移 提交于 2020-01-09 09:38:07
问题 I would like to have in Java SE @Stateless public class CarDAO { @Inject private EntityManager em; public Car findById(Long id) { return em.find(Car.class, id); } } @Singleton public class Application { @Inject private CarDAO carDAO; public void run() { Car car = carDAO.findById(44); System.out.println(car); } } public class EntryPoint { public static void main(String[] args) { Application application = // missing code application.run(); } } What I have to do to achieve that? I'm using

Getting a reference to EntityManager in Java EE applications using CDI

人盡茶涼 提交于 2020-01-09 08:43:31
问题 I'm using Java EE 7. I would like to know what is the proper way to inject a JPA EntityManager into an application scoped CDI bean. You can't just inject it using @PersistanceContext annotation, because EntityManager instances are not thread safe. Let's assume that we want our EntityManager to be created at the beginnig of every HTTP request processing and closed after the HTTP request is processed. Two options come into my mind: 1. Create a request scoped CDI bean which has a reference to an

Entitymanager throwing NullPointerException

拟墨画扇 提交于 2020-01-07 04:55:09
问题 Am developing a JavaEE application using Netbeans.The Entity Manager is getting a null value in the application. The em is not getting injected. Still am getting a nullPointerException as em is getting a null value in createNamedQuery. Could anyone please let me know what am I missing. JSP page- index.jsp: <%@page import="managed.userBean" %> <jsp:useBean id="bean" class="managed.userBean" scope="session" /> ..... <input type="submit" value="Submit" onclick="<jsp:scriptlet> bean.validate();<

JPQL: why 'group by' does not work properly

ε祈祈猫儿з 提交于 2020-01-07 03:45:07
问题 Executing JPQL using hibernate 4.2.5: select c from Chargeback as cc join cc.customer as c group by c order by max(cc.created) Usecase: show customers with the latest chargbacks. produces org.hibernate.exception.SQLGrammarException: could not extract ResultSet When using select c.id from Chargeback as cc join cc.customer as c group by c order by max(cc.created) it will work. When using select c from Chargeback as cc join cc.customer as c group by c.id, c......all columns order by max(cc

How can I fetch every row where column is not in (array of values) with Doctrine?

戏子无情 提交于 2020-01-06 19:44:08
问题 I'm trying to accomplish something similar to the following SQL, but using the Doctrine API. SELECT * FROM table_name WHERE column_name NOT IN (1, 2, 3); I was thinking of something like this: $entityManager->getRepository($entity)->findBy(array('ID' => array('NOT IN' => array(1, 2, 3)))); ... How far am I? 回答1: You can do this with a DQL: $result = $entityManager->createQuery("SELECT e FROM $entity e WHERE e.ID NOT IN (:ids)") ->setParameter('ids', array(1, 2, 3), \Doctrine\DBAL\Connection:

EntityManager is null inside Pojo class in ejb container

前提是你 提交于 2020-01-06 15:04:36
问题 I have a Java web service module and ejb modulein netbeans (All part of an enterprise application). the web service is consuming the ejb class using @EJB injection. Inside the ejb module i have a TransactionManager class which is not an enterprise bean. just a POJO class. I am trying to inject the EntityManager using @PersistanceContext(unitName = "testPU") EntityManager em; but the em is allways null. I am calling the TransactionManager class from by bean, and if i declare the EntityManager