aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes @OneToMany return objects with the same reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "@OneToMany return objects with the same reference " Watch "@OneToMany return objects with the same reference " New topic
Author

@OneToMany return objects with the same reference

Mohammad wish
Greenhorn

Joined: Oct 11, 2011
Posts: 12
Dear Professional,
i have an Entity which have the parentAccount @OneToMany attribute (it is a foreign Key to the primary key )

public class AccountBean implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "accountGenerator")
@Column(name = "ID")
private Integer id;

@Column(name = "NAME_AR", length = 100, unique = false)
private String nameAr;

@ManyToOne
@JoinColumn(name = "LEVEL_ID",nullable = false, referencedColumnName = "ID")
private LevelBean level;

@ManyToOne
@JoinColumn(name = "PARENT_ACCOUNT_ID",nullable = true, referencedColumnName = "ID")
private AccountBean parentAccount;


execute JPA query to get all accounts , i notice that all parentAccount object is reference to the same object for all accounts that have same parent account i.e ( i have 3 accounts and all of them have the same parent account object , in the memory the 3 accounts reference the same parent account object in the memory )

the problem is if in my screen once i changed the parent account of one of the 3 account the other parent account changed for the other accounts (because they are reference to the same object in the memory ), how can i solve the problem.

Regards
Wish79
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

i notice that all parentAccount object is reference to the same object for all accounts that have same parent account

That would be the expected behaviour, why would you expect something different?
Mohammad wish
Greenhorn

Joined: Oct 11, 2011
Posts: 12
I know but my screen submit all records so when I changed one of them the others ( which have the same parent account reference object ) changed , the expected behavior should changed just the changed record not all
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

But you have changed the one and only parent record. You would want all child records to see the update surely?
Mohammad wish
Greenhorn

Joined: Oct 11, 2011
Posts: 12
I want just the changed record to be affected , and the other records still have the old value not new one
Mohammad wish
Greenhorn

Joined: Oct 11, 2011
Posts: 12
i solved the problem by setting the parentaccount object to null, then update it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: @OneToMany return objects with the same reference