简体   繁体   中英

How to get Eclipselink Entity Manager?

I'm working on a C/S java project and try to use Eclipselink to do the ORM(client side cache database,derby). It's quite easy to do the query with examples & expressions, but when it comes to insert/update, i found that Entity Manager should be used(I'm quite new in java ORM, maybe there's another way?). When i try to create em, it always throw a Exception that telling me i need a server session(ie can not find the persistence-unit by name). When i use session bean & @PersistenceUnit annotation, it returns a Null.

Did I make some stupid mistakes? Or does there any easy way to Update/Insert values? I supposed that there may be some way like session.save(Object) or something like that which can makes this work simple.

EDIT: I'v tried to use UnitOfWork, but get "Attempt to modify an identity column" error, should i change map file or something that make Eclipselink recognize the identity PK?

@GeneratedValue(strategy = GenerationType.IDENTITY)

Add this line in POJO do not work.

EDIT :I've tried use Unit of work to insert value here's the code

    SessionFactory sessionFactory = new SessionFactory("default");
    Session session = sessionFactory.getSharedSession();
    UnitOfWork uow = session.acquireUnitOfWork();
    SysUser usr2 = new SysUser();
    usr2.setUserName("test");
    usr2.setUserPassword("test");
    uow.registerObject(usr2);
    uow.commit();

But it result in a

Internal Exception: java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'DB_ID'.

here's the annotation i used:

@Column(name = "DB_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
protected int dbId;

And for EntityManager:

<persistence-unit name="my-app-name">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/app/jdbc/jdbc/ConnectionDerbyDS</jta-data-source>

When you use EclipseLink (via the JPA API) in a Java SE application, you have to set up things a little differently then when your app consists of components running in a Java EE container.

You get an EntityManager with lines like these:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-app");
EntityManager em = emf.createEntityManager();

And your application needs to have a persistence.xml file in the META-INF directory of the JAR file of your application. You configure JPA in this file; things you put in there tell Java which persistence provider to use and what the connection parameters for your database are.

The persistence unit name ( "my-app" in the example above) must be the same as the name specified in the persistence-unit element in persistence.xml .

See this EclipseLink example , which shows what your persistence.xml has to look like.

EntityManager contains methods such as persist(Object) which you can use to save entities into the database (see these examples that show how to use EntityManager ).

Your entity POJO should have a column which will contain the entity id, and it must have a no-args constructor:

@Entity
public class Example {

    @Id
    @GeneratedValue
    private long id;

    // ... Add other properties here

    // Required no-args constructor
    public Example() {
    }

    // ... Add getters and setters here
}

Addition

You can save an entity in the database using the EntityManager like in the following code. No need to use UnitOfWork (as far as I know, UnitOfWork is something you needed when using the older JPA 1.0 API; you don't need this for JPA 2.0):

entityManager.getTransaction().begin();
entityManager.persist(myEntity);
entityManager.getTransaction().commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM