File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes @OneToMany Example In JPA with transaction type JTA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "@OneToMany Example In JPA with transaction type JTA" Watch "@OneToMany Example In JPA with transaction type JTA" New topic
Author

@OneToMany Example In JPA with transaction type JTA

Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Hi All,

I need a sample code for @OneToMany relationship.

i have a entity as follows:

Employee.java




PhoneNumber.java



bean is getting deployed successfully in jboss5.1.0.GA without any problem and table is getting created.

I have EmployeeFacade Stateless Session Bean as follows:


PhoneNumberFacade Stateless Session Bean as follows:



both EmployeeFacade, PhoneNumberFacade inherits :



persistence.xml



Client :



ERROR [AbstractFlushingEventListener] Could not synchronize database state with session

and

Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("EXAMPLE"."PHONENUMBER"."EMPLOYEE_ID")

at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10656)
at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:774)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 39 more


Please help me to resolve this problem.

Thanks,
Bennet Xavier X.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

In my copybook where I am writing interesting notes it is written:
To be an entity class must follow these rules:
1. class must be annotated with @Entity
2. class must contain primary key annotated by @Id
3. class must contain public or protected no-argument constructor
4. class annotated by @Entity must be top-level, class without implementing or extending something
5. entity class must be not final, must not contain final variable or methods.
Also it is written:
Persistence provider maps entity to table thanks to annotation.
Do you have persistence provider configured in persistence.xml. If not then look at my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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/persistence_2_0.xsd">
<persistence-unit name="OlympicsPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/olympicsDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/olympics?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="123456"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
</properties>
</persistence-unit>
</persistence>


True person is moral, false is right!
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

Hi Bennet,
In your case Employee is the parent and PhoneNumber is the child. So, the usual way to do is to create the parent object and add the child objects via the list and then persist the parent object (rather than the child object).
In your case, you should persist the Employee object via the EmployeeFacade (and the PhoneNumber object(s) should be added to the List<PhoneNumber>).


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @OneToMany Example In JPA with transaction type JTA