Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Error writing collection of entities to the database

./tch



On Wed, Jan 21, 2009 at 7:06 PM, krisusa123 <krisusa123@xxxxxxxxx> wrote:
> Hi Christopher,
> I can do that but thats row by row processing and having each row as a
> seperate transaction. Is there any way I can do the same thing that the
> below eclipselink wiki describes.

You can use batch writing if your JDBC driver supports it -- see
http://wiki.eclipse.org/Optimizing_the_EclipseLink_Application_(ELUG)#How_to_Use_Batch_Writing_for_Optimization


>
> I want to read all the entity objects that are in my arraylist and write
> them to the database because I know the data can be blindly dumped into the
> target table without checking any constraints.
>
> My case happends to be where the arraylist is populated from xml files
> rather than getting data from the db (which is what the below code is
> doing). FYI: J2SE environment is what I am working in.

Ok, so you are not starting out with entities that are persistable the
way you'd like them to be. I would think (I don't have much experience
with the xml stuff) that you could copy your entities from the xml
entities, to new db-linked entities, and persist each one.
>
> The List employees is populated from the source db and dumped into the
> target db in one transaction. I should be able to do the same thing even if
> I bring data from non db's.

Yes you can, it's just a question of how much code you want to write.
The native example you point to below, works so elegantly because
you're always dealing with entities you got out of the database to
begin with. You can do a similar thing, but you'd have to copy your
values to your new db backed entity first.

Psuedo-code:
Get XML Entities
foreach xml entity
-copy values to new Entity (by hand or with something like beanutils)
-call persist with each new entity.

If you use batch writing, this will be more efficient -- look up JDBC
batch processing for more info.

Now, that being said, I'll add my two-cents. What you're attempting to
do with EclipseLink is certainly something you can do. However, it's
not a strong suit of EL. You've got a lot of overhead, with
registering each new entity in the EM, and all the caching, etc. that
goes with that.

If you really want to do a straight DB dump, just use pure JDBC!
Remember, JPA isn't a JDBC replacement, but a tool you can use with
JDBC to make your life easier in certain situations.


>
> http://wiki.eclipse.org/Optimizing_the_EclipseLink_Application_%28ELUG%29#Example_11-16
>
> Native API
>
> // Read all the employees, acquire a unit of work, and register them
>
> // Read all the employees from the database. This requires 1 SQL call,
> // but will be very memory intensive as 10,000 objects will be read
> List employees = sourceSession.readAllObjects(Employee.class);
>
> //SQL: Select * from Employee
>
> // Acquire a unit of work and register the employees
> UnitOfWork uow = targetSession.acquireUnitOfWork();
> uow.registerAllObjects(employees);
> uow.commit();
>
> f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng
>
> --- On Wed, 1/21/09, Christopher Delahunt <CHRISTOPHER.DELAHUNT@xxxxxxxxxx>
> wrote:
>
> From: Christopher Delahunt <CHRISTOPHER.DELAHUNT@xxxxxxxxxx>
> Subject: Re: [eclipselink-users] Error writing collection of entities to the
> database
> To: "Eclipselink-Users" <eclipselink-users@xxxxxxxxxxx>, "Krisusa123"
> <krisusa123@xxxxxxxxx>
> Date: Wednesday, January 21, 2009, 5:58 PM
>
> Hello,
>
> Your are geting the exception in all cases because you are calling persist
> on an ArrayList.  Persist can only be called on Entities, not collections.
> Your code can just call em.persist on each TestEtl you create in the for
> loop, assuming TestEtl is an entity class.
>
> Regards,
> Chris
> ----- Original Message -----
> From: "Krisusa123" <krisusa123@xxxxxxxxx>
> To: "Eclipselink-Users" <eclipselink-users@xxxxxxxxxxx>, "Gordon Yorke"
> <GORDON.YORKE@xxxxxxxxxx>
> Sent: Wednesday, January 21, 2009 5:20:25 o'clock PM (GMT-0500)
> America/New_York
> Subject: Re: [eclipselink-users] Error writing collection of entities to the
> database
>
> My code has commit after em.persist..
>
> I still get error of this type because I am trying to commit arraylist of my
> entity classes rather than an object of my entity class.
>
>
> java.lang.IllegalArgumentException
>
> : Object: [com.xx.entities.TestEtl@1da669c, com.xx.TestEtl@186c730,
> com.xx.entities.TestEtl@17f242c] is not a known entity type.
>
> at
> org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(
>
> UnitOfWorkImpl.java:3820)
>
> at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(
>
> EntityManagerImpl.java:219)
>
> at
> com.thomson.west.nal.warehouse.entitymanager.EntityManagerProcessor.saveData(
>
> EntityManagerProcessor.java:142)
>
> f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng
>
> --- On Wed, 1/21/09, Gordon Yorke <gordon.yorke@xxxxxxxxxx> wrote:
>
> From: Gordon Yorke <gordon.yorke@xxxxxxxxxx>
> Subject: Re: [eclipselink-users] Error writing collection of entities to the
> database
> To: krisusa123@xxxxxxxxx, "EclipseLink User Discussions"
> <eclipselink-users@xxxxxxxxxxx>
> Cc: "Tom Ware" <tom.ware@xxxxxxxxxx>
> Date: Wednesday, January 21, 2009, 12:48 PM
>
> em.persist() doesn't actually write directly to the database and calling it
> multiple times would have the same effect as calling persist on a list.
> The Inserts do no occur until the em is flushed or the transaction
> committed.
> --Gordon
>
> krisusa123 wrote:
>
> Hi Tom
> Thanks for the reply. But, thats too much of I/O where it commits row by row
> rather than commiting a collection. I do understand that it needs to check
> referential integrity and database constraints.
>
> Is there any way out of this?
>
> Thanks
> kris
>
> f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng
>
> --- On Wed, 1/21/09, Tom Ware <tom.ware@xxxxxxxxxx> wrote:
>
> From: Tom Ware <tom.ware@xxxxxxxxxx>
> Subject: Re: [eclipselink-users] Error writing collection of entities to the
> database
> To: krisusa123@xxxxxxxxx, "EclipseLink User Discussions"
> <eclipselink-users@xxxxxxxxxxx>
> Date: Wednesday, January 21, 2009, 7:53 AM
>
> Hi Kris,
>
>   The code that uses UnitOfWork is not JPA code.  UnitOfWork is an
> EclipseLink
> class and not necessary when using pure JPA.  Is there any particular reason
> you
> have written that code?
>
>   The rest of the code uses JPA APIs.
>
>   The problem you are seeing is because you are trying to persist your array
> of
> TestEtl as a whole instead of individually.   You can only call persist on
> classes that are persistent.  List is not a persistent class.  The classes
> stored in your list are the classes you want to persist.
>
>   You should write code that iterates through your list and persists each
> element individually.
>
> -Tom
>
> krisusa123 wrote:
>> Hello Guys
>> Now I have a new problem with my pure JPA implementation.
>> I am trying to commit a collection (ArrayList of entity objects) rather
> than an entity object and here is the error I am getting. Can I do this or
> is it
> wrong?
>>  Code also attached below.
>>  Appreciate your help.
>>  Exception in thread "main"
> _java.lang.IllegalArgumentException_: Object:
> [com..entities.TestEtl@1da669c,
> com.entities.TestEtl@186c730 <mailto:com.entities.TestEtl@186c730>,
> com.entities.TestEtl@17f242c <mailto:com.entities.TestEtl@17f242c>] is not
> a known entity type.
>>  * *
>> *public* *static* *boolean* saveData(EntityManagerFactory emf,
> List<xx> Beans ) {
>>
>> EntityManager entityManager = emf.createEntityManager();
>>
>> UnitOfWork uow = ((EntityManagerImpl) entityManager).getUnitOfWork();
>>
>> List<TestEtl> lm = *new* ArrayList<TestEtl>();
>>
>> uow.registerAllObjects(lm);
>> * *
>>
>> *try* {
>>
>> *for* (*int* i=0; i< Beans.size(); i++) {
>>
>> TestEtl load = *new* TestEtl();
>>
>> load.setCol1(Beans.get(i).getCol1());
>>
>> load.setCol2(Beans.get(i).getCol2());
>> lm.add(load);
>>
>> } *catch* (Exception e) {
>>
>> System./out/.println( "Error::" + e);
>>
>> *return* *false*;
>>
>> } *finally* {
>>
>> entityManager.persist(lm);
>>
>> uow.commit();
>>
>> entityManager.close();
>>
>> emf.close();
>> }**
>>
>> *return* *true*;
>>
>> }
>>
>> f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng
>>
>> --- On *Tue, 1/20/09, krisusa123 /<krisusa123@xxxxxxxxx>/* wrote:
>>
>>     From: krisusa123 <krisusa123@xxxxxxxxx>
>>     Subject: Re: [eclipselink-users] How to create EclipseLink Sessions,
>>     ORM.xml files in J2SE environment
>>     To: "EclipseLink User Discussions"
> <eclipselink-users@xxxxxxxxxxx>
>>     Date: Tuesday, January 20, 2009, 10:11 AM
>>
>>     Hi Tim
>>     What you said was pretty much true until yesterday. I had got
>>     confused between pure JPA solution and EclipseLink specific
>>     implementation.
>>          But now, I have got the pure JPA solution working. Thanks to you
> for
>>     helping me finish that part.
>>          Then I started trying to accomplish the same thing using
> EclipseLink
>>     only API by using sessions.xml and stuff.
>>          I am trying to understand how to get the second one (EclipseLink
>>     specific one) working. Thats where I got this exception:
>>          Exception Description: Missing descriptor for [class
> com.xx.TestEtl].
>>     Verify that the descriptor has been properly registered with the
>>     Session.
>>
>>
>>     f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng
>>
>>     --- On *Tue, 1/20/09, Tim Hollosy /<hollosyt@xxxxxxxxx>/* wrote:
>>
>>         From: Tim Hollosy <hollosyt@xxxxxxxxx>
>>         Subject: Re: [eclipselink-users] How to create EclipseLink
>>         Sessions, ORM.xml files in J2SE environment
>>         To: "EclipseLink User Discussions"
> <eclipselink-users@xxxxxxxxxxx>
>>         Date: Tuesday, January 20, 2009, 9:27 AM
>>
>>         Also be aware, the example on my blog uses a pure JPA approach
>>         (mostly) the only native class I use is UnitOfWork. I think Kris
> is
>>         kind of confused between the two. But just bare in mind, if
> you're
>>         following my example/my code make sure you realize you're
> working in a
>>         Pure JPA environment.
>>
>>         ./tch
>>
>>
>>
>>         On Tue, Jan 20, 2009 at 9:06 AM, Tom Ware
> <tom.ware@xxxxxxxxxx> wrote:
>>         > Hi Kris,
>>         >
>>         >  This error generally occurs because for some reason the
> metadata for your
>>         > TestEtl class is not available to EclipseLink and you are
> treating it as
>>         > persistent.
>>         >
>>         >  Depending on how you are configured, the metadata can come
> from different
>>         > places.
>>         >
>>         >  If you are using a pure JPA solution, it will be derived
> from
>>         Annotations,
>>         > orm.xml and your persistence.xml file.
>>         >
>>         >  If you are using the EclipseLink-proprietary APIs, it will
> be derived
>>         from
>>         > your sessions.xml and from either your project class or your
> deployment
>>         > xml(generated with the EclipseLink Mapping Workbench)
>>         >
>>         >  Perhaps you can explain a little more about which of these
> files you are
>>         > using and what you are doing when you see the error.
>>         >
>>         > -Tom
>>         >
>>         > krisUSA123 wrote:
>>         >>
>>         >> Based on EclipseTeam's suggestions, I was able to
> create
>>         sessions.xml file
>>         >> and was trying to create a CRUD operation.
>>         >> May I know what this error means:
>>         >>
>>         >> Exception Description: Missing descriptor for [class
> com.xx.TestEtl].
>>         >> Verify that the descriptor has been properly registered
> with the
>>         Session.
>>         >>
>>         >> How do I fix it?
>>         >>
>>         >> Appreciate your help.
>>         >> Thanks guys for being responsive to my earlier post.
>>         >>
>>         >> Thanks
>>         >> Kris
>>         >
>>         > _______________________________________________
>>         > eclipselink-users mailing list
>>         > eclipselink-users@xxxxxxxxxxx
>>         > https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>         >
>>         _______________________________________________
>>         eclipselink-users mailing list
>>         eclipselink-users@xxxxxxxxxxx
>>         https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>
>>
>>     _______________________________________________
>>     eclipselink-users mailing list
>>     eclipselink-users@xxxxxxxxxxx
>>     https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
>
> ________________________________
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
>
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
>


Back to the top