| Author |
One To One Mapping not working, plz help me out
|
Gautam Ry
Ranch Hand
Joined: Dec 30, 2008
Posts: 41
|
|
Hi I am new in EJB3.0. I have written a simple code but is showing problem. please,help me........ and thanks for your time Over View of the development Application Server: weblogic10.0. Database: MS SQL 2000 Entity Bean: Address.java and Employee.java (OneToOne mapping and Navigability: bidirectional ) Stateless Bean: EmployeeAddressUniBean.java Remote Interface: EmployeeAddress the error, I am getting javax.ejb.EJBException: EJB Exception: ; nested exception is: <openjpa-1.1.0-r422266:657916 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Type "com.kogent.one_to_one.Address" attempts to use both field and property access. Only one access method is permitted. Field access is used on the following fields: [private int com.kogent.one_to_one.Address.id, private int com.kogent.one_to_one.Address.id]. Property access is used on the following methods: [public com.kogent.one_to_one.Employee com.kogent.one_to_one.Address.getEmployee()]. at weblogic.ejb.container.internal.RemoteBusinessIntfProxy.unwrapRemoteException(RemoteBusinessIntfProxy.java:105) at weblogic.ejb.container.internal.RemoteBusinessIntfProxy.invoke(RemoteBusinessIntfProxy.java:87) Caused by: <openjpa-1.1.0-r422266:657916 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Type "com.kogent.one_to_one.Address" attempts to use both field and property access. Only one access method is permitted. Field access is used on the following fields: [private int com.kogent.one_to_one.Address.id, private int com.kogent.one_to_one.Address.id]. Property access is used on the following methods: [public com.kogent.one_to_one.Employee com.kogent.one_to_one.Address.getEmployee()]. my code is as below: Address.java package com.kogent.one_to_one; import javax.persistence.*; @Entity(name="Address") public class Address implements Serializable { @Id @Column(name = "ADDRESSID", nullable = false) private int id; private String city; private String zipcode; private Employee employee; public int getId() { return id; } public void setId(int id) { this.id=id; } public String getCity() { return city; } public void setCity(String city) { this.city=city; } public String getZipcode() { return zipcode; } public void setZipcode(String zipcode) { this.zipcode=zipcode; } @OneToOne(mappedBy="Address") public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee=employee; } } Employee.java package com.kogent.one_to_one; import javax.persistence.*; @Entity @Table(name="EMPLOYEE") public class Employee implements Serializable { @Id @Column (name = "EMPID", nullable = false) private int id; private String empName; @OneToOne @JoinColumn(name = "ADDRESSID", referencedColumnName = "ADDRESSID") private Address empAddress; public int getId() { return id; } public void setId(int id) { this.id=id; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName=empName; } @OneToOne(cascade={CascadeType.ALL}) public Address getEmpAddress() { return this.empAddress; } public void setEmpAddress(Address empAddress) { this.empAddress = empAddress; } } EmployeeAddressUniBean.java package com.kogent.one_to_one; import javax.ejb.Stateless; import javax.persistence.*; @Stateless public class EmployeeAddressUniBean implements EmployeeAddress { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence"); EntityManager entityManager = entityManagerFactory.createEntityManager(); /* @PersistenceContext(unitName = "persistence") EntityManager entityManager;*/ public void doSomeStuff() { Address ad=new Address(); ad.setId(1001); ad.setCity("Delhi"); ad.setZipcode("Zip1"); Employee emp=new Employee(); emp.setId(109); emp.setEmpName("Anil"); emp.setEmpAddress(ad); entityManager.persist(emp); } public List getEmployee() { Query q=entityManager.createQuery("SELECT e from EMPLOYEE e"); return q.getResultList(); } } persistence.xml <?xml version="1.0"?> <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/persistence_1_0.xsd" version="1.0"> <persistence-unit name="persistence" transaction-type="JTA"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <jta-data-source>jdbc/customer</jta-data-source> <class>com.kogent.one_to_one.Employee</class> <class>com.kogent.one_to_one.Address</class> <properties> <property name="openjpa.ConnectionURL" value="jdbc dbc:jdbc/customer" /> <property name="openjpa.ConnectionDriverName" value="sun.jdbc.odbc.JdbcOdbcDriver" /> <property name="openjpa.ConnectionUserName" value="" /> <property name="openjpa.ConnectionPassword" value="" /> </properties> </persistence-unit> </persistence> regards gautam
|
 |
aswin kumar
Greenhorn
Joined: Dec 25, 2008
Posts: 12
|
|
hi, just check the below line you gave, @OneToOne(mappedBy="Address") ,you have to give property name association of the ownerSide(in your case empAddress)just change and check, hope this solves your problem regards
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8143
|
|
javax.ejb.EJBException: EJB Exception: ; nested exception is: <openjpa-1.1.0-r422266:657916 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Type "com.kogent.one_to_one.Address" attempts to use both field and property access. Only one access method is permitted. Field access is used on the following fields: [private int com.kogent.one_to_one.Address.id, private int com.kogent.one_to_one.Address.id]. Property access is used on the following methods: [public com.kogent.one_to_one.Employee com.kogent.one_to_one.Address.getEmployee()].
Its really good to see such a clear error message in the exception stacktrace. The error messsage points out that in your code (entities) you are using both field access as well as property access. What this means is, for the entities you are annotation both the member variables as well as the methods . This is not allowed in JPA. You are either allowed to annotate the field (the member variable) or the method. So you can move the annotations from the field to property access on your entities to fix this.
|
[My Blog] [JavaRanch Journal]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56192
|
|
Please use real words when posting to the forums. Abbreviations such as "plz" in place of "please" only serve to make your posts more difficult to read and less likely to generate useful responses. Please read this for more information. thanks, bear JavaRanch sheriff
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Gautam Ry
Ranch Hand
Joined: Dec 30, 2008
Posts: 41
|
|
Thank You Big Boss,Jaikiran Pai (Member # 103156). Your Post on this issue was the absolutely correct. My code is working fine after changing as You told. You are my God at this moment.I spend almost seven days. Thanks to aswin kumar(Member# 189563) too. By the by, I have a Query. There is a node <provider> in the persistence.xml. What's that actually? And How to configure that one? I am using "sun.jdbc.odbc.JdbcOdbcDriver" -driver for data base(MS SQL Server-200) connection. If time permits, please...... "Happy New Year To Both Of You" regards Gautam
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8143
|
|
There is a node < provider> in the persistence.xml
The persistence.xml is a JPA standard file. There are multiple providers (implementations) who follow the JPA spec. Some of the examples are Hibernate and TopLink. The <provider> element in the persistence.xml can be used to specify the fully qualified classname of the JPA implementation provider.
|
 |
 |
|
|
subject: One To One Mapping not working, plz help me out
|
|
|