entitymanager

EntityManager does not write to database

我们两清 提交于 2019-11-26 21:24:13
问题 i just set up a so far still quite minimal project maven/jpa/hibernate project, where i am trying to persist an object. My class is a quite simple one: @Entity public class Person { @Id @GeneratedValue private int id; private String name; } My persistence.xml is very basic as well: <persistence 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

No Persistence provider for EntityManager named X

血红的双手。 提交于 2019-11-26 21:02:54
I am developing a JavaSE application using JPA. Unfortunately, I get null after calling: Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); Below you will find: A snippet of my code that invokes EntityManagerFactory and unexpectedly returns null My persistence.xml file My project structure Snippet of my code: public class Main { private static final String PERSISTENCE_UNIT_NAME = "MeineJpaPU"; private static EntityManagerFactory factory; public static void main(String[] args) { // I get null on this line!!! factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

Create JPA EntityManager without persistence.xml configuration file

江枫思渺然 提交于 2019-11-26 19:29:23
Is there a way to initialize the EntityManager without a persistence unit defined? Can you give all the required properties to create an entity manager? I need to create the EntityManager from the user's specified values at runtime. Updating the persistence.xml and recompiling is not an option. Any idea on how to do this is more than welcomed! Arthur Ronald Is there a way to initialize the EntityManager without a persistence unit defined? You should define at least one persistence unit in the persistence.xml deployment descriptor. Can you give all the required properties to create an

Using EntityManager inside Doctrine 2.0 entities

橙三吉。 提交于 2019-11-26 18:45:01
I have 2 entities: Country (id, name) and Mapping (id, object, internalId, externalId). Country and Mapping are not connected with associations (because Mapping has rows not only for country). I need to get external id for country using following conditions: country.id = mapping.internalId mapping.object = 'country' So I plan to add function getExternalId() in Country function getExternalId() { $em = Registry::getEntityManager(); $mapping = $em->getRepository('Mapping')->findOneBy(array( 'object' => 'country', 'internalId' => $this->getId() )); return !empty($mapping) ? $mapping->getExternalId

PersistenceUnit vs PersistenceContext

梦想与她 提交于 2019-11-26 17:59:29
问题 In few project I have been successfully using @PersistenceUnit(unitName = "MiddlewareJPA") EntityManagerFactory emf; ... EntityManager entityManager = emf.createEntityManager(); to obtain EntityManager for Database connection, but some days ago I was trying to move my project to Jboss EAP 6.2 and it couldn't create EntityManager . I was googling it and I found that I should try change @PersistenceUnit to @PersistenceContext(unitName = "MiddlewareJPA") private EntityManager entityManager; to

What is the difference between EntityManager.find() and EntityManger.getReference()?

谁说我不能喝 提交于 2019-11-26 17:57:09
问题 Whats is the difference between <T> T EntityManager.find(Class<T> entityClass, Object primaryKey) and <T> T EntityManager.getReference(Class<T> entityClass, Object primaryKey) ? I think getReference returns entity if it is managed. and find returns entity if it is managed else executes SQL on database to make it managed. Please confirm. Context: From webapp I get primary key of object to be deleted (pk of type long); to entity should be managed to delete. EntityManager.remove(Object entity)

createEntityManager throws java.lang.NullPointerException at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus

别说谁变了你拦得住时间么 提交于 2019-11-26 16:53:37
问题 I'm getting this stack trace when I deploy my hibernate app java.lang.NullPointerException at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:72) [hibernate-core-4.0.0.Beta1.jar:4.0.0.Beta1] at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:114) [hibernate-core-4.0.0.Beta1.jar:4.0.0.Beta1] at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149) [hibernate-core-4.0.0

How to inject persistence context to different data source programmatically

二次信任 提交于 2019-11-26 16:29:32
问题 In standard EJB 3, when injecting entity manager, persistence unit (which refers to datasource) is hardcoded into annotation: (or alternatively xml file) @PersistenceContext(unitName = "myunit") private EntityManager entityManager; Is there a way to use an entity manager but to select data source by name at runtime? 回答1: Using EclipseLink, You can set a DataSource configured in your app server. import org.eclipse.persistence.config.PersistenceUnitProperties; ... .... Map props = new HashMap()

What does EntityManager.flush do and why do I need to use it?

倖福魔咒の 提交于 2019-11-26 15:37:02
问题 I have an EJB where I am saving an object to the database. In an example I have seen, once this data is saved (EntityManager.persist) there is a call to EntityManager.flush(); Why do I need to do this? The object I am saving is not attached and not used later in the method. In fact, once saved the method returns and I would expect the resources to be released. (The example code does this on a remove call as well.) if (somecondition) { entityManager.persist(unAttachedEntity); } else {

The EntityManager is closed

与世无争的帅哥 提交于 2019-11-26 11:59:00
问题 [Doctrine\\ORM\\ORMException] The EntityManager is closed. After I get a DBAL exception when inserting data, EntityManager closes and I\'m not able to reconnect it. I tried like this but it didn\'t get a connection. $this->em->close(); $this->set(\'doctrine.orm.entity_manager\', null); $this->set(\'doctrine.orm.default_entity_manager\', null); $this->get(\'doctrine\')->resetEntityManager(); $this->em = $this->get(\'doctrine\')->getEntityManager(); Anyone an idea how to reconnect? 回答1: This is