Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Object Relational Mapping
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Object Relational Mapping
Problem at insert with relationship JPA 2.0
Miltos Tereres
Ranch Hand
Posts: 32
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is my Employee entity
public class Employee implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @Lob private ArrayList<String> rights; @ManyToOne private EmployeeGroups group; @OneToOne(mappedBy="employee",cascade=CascadeType.PERSIST) private Account account; public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Employee)) { return false; } Employee other = (Employee) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return "gr.medilab.personnel.data.Employee[id=" + id + "]"; } /** * @return the rights */ public ArrayList<String> getRights() { return rights; } /** * @param rights the rights to set */ public void setRights(ArrayList<String> rights) { this.rights = rights; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the group */ public EmployeeGroups getGroup() { return group; } /** * @param group the group to set */ public void setGroup(EmployeeGroups group) { this.group = group; } /** * @return the account */ public Account getAccount() { return account; } /** * @param account the account to set */ public void setAccount(Account account) { this.account = account; } public boolean hasAccount() { return getAccount()!=null; } }
Here is my Account Entity
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package gr.medilab.model.personnel.data; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="Accounts") public class Account implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @OneToOne private Employee employee; private String username; private String password; public boolean passwordMatch(String password) { return true; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Account)) { return false; } Account other = (Account) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return "gr.medilab.model.personnel.data.Account[id=" + id + "]"; } /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the employee */ public Employee getEmployee() { return employee; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } }
When i try to add a new employee
Employee e=new Employee(); e.setGroup(pousts); e.setName("Pisoglentis"); ArrayList<String> piso=new ArrayList<String>(); piso.add("malakas"); piso.add("pousti"); Account ac=new Account(); ac.setUsername("Doroula"); e.setAccount(ac); e.setRights(piso); em.persist(e); tx.commit(); em.close(); emf.close();
in the database at the ACCOUNT table i get EMPLOY_ID NULL
why does this happen? what can i do ?
Thank you for your time...
James Sutherland
Ranch Hand
Posts: 553
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You need to set the employee in the Account (you need a setEmployee() method in Account).
See,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side
TopLink
:
EclipseLink
:
Book:Java Persistence
:
Blog:Java Persistence Performance
Miltos Tereres
Ranch Hand
Posts: 32
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank you for the reply...
I am trying to understand why it works only from the owner side in one to one relationships...
I will read again your link...
so doing ac.setemploye() only instead of e.setaccount it would work?
i ll try...thanks...
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JPA + Glassfish + Netbeans+enity class + generate tables =(
JPA returning collection of type PersistentBag instead of ArrayList
Is the statement about EJB 3.x right?
Beans binding with local variable
hashCode() & equals() method test
More...