|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Toplink cascade questionsHi all,
I have questions about the relationships. My situation: I have two entities Client and Address. One Client can have many Addresses, so the relationship is one-to-many and Address contains the key of a Client as a foreign key. Address can not exist without Client. I use Netbeans, MySQL, Toplink, Glassfish and EJB 3.0. Application is a swing client, so I'm using deattached entities. Here are my questions (maybe I'm doing something wrong): 1. If I want to add an Address to Client, why I have to set foreign key in Address? Acctually in java code it's not foreign key(integer), it's the whole Client entity object. If I don't set Client object manually (foreign key is null), I've got exception, that the client foreign key in address cannot be null. Can Toplink handle the foreign key in Address automatically? 2. When I update a value in Address (for example street), I set it into Address list in client and update client, the address is not updated. I update the equlas method to compare street, city and other attributes of Address, but it doesn't help. So I see two ways: delete all Addresses from Client and add them with updated values, or update the Address separately outside the Client. Can toplink handle this without manuall coding? Here is my code: Code: // Client entity public class Client implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "idClient") private Integer idClient; @Column(name = "name") private String name; @Column(name = "surname") private String surname; @OneToMany(cascade = CascadeType.ALL, mappedBy = "clientidClient", fetch = FetchType.EAGER) private List<Address> addressList; ... // Address entity public class Address implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "idAddress") private Integer idAddress; @Column(name = "Street") private String street; @Column(name = "Number") private String number; @Column(name = "City") private String city; @Column(name = "Postcode") private Integer postcode; @Column(name = "Country") private String country; @JoinColumn(name = "Client_idClient", referencedColumnName = "idClient") @ManyToOne(cascade = CascadeType.ALL, optional = false) private Client clientidClient; ... |
|
|
Re: Toplink cascade questionsAnaq, In regards to your questions, here are my situation: 1. I don't think Toplink, though I never used it, can handle it automatically for you. At the time of the Address creation, since it can't exist without a proper Client, you have to know which Client it would be assigned to. My advice though, it would be easier, if you can lift the requirement of an address not existing without a client. In that way, you can create address with 'null' value for client and later on you can set its client value. 2. For this question I would recommend you set you db with with 'cascade update'. Here a good link about it: http://www.dba-oracle.com/oracle_tips_cascade_update.htm From: anaq <anaque@...> To: nbj2ee@... Sent: Thu, October 29, 2009 10:19:59 AM Subject: [nbj2ee] Toplink cascade questions Hi all, I have questions about the relationships. My situation: I have two entities Client and Address. One Client can have many Addresses, so the relationship is one-to-many and Address contains the key of a Client as a foreign key. Address can not exist without Client. I use Netbeans, MySQL, Toplink, Glassfish and EJB 3.0. Application is a swing client, so I'm using deattached entities. Here are my questions (maybe I'm doing something wrong): 1. If I want to add an Address to Client, why I have to set foreign key in Address? Acctually in java code it's not foreign key(integer), it's the whole Client entity object. If I don't set Client object manually (foreign key is null), I've got exception, that the client foreign key in address cannot be null. Can Toplink handle the foreign key in Address automatically? 2. When I update a value in Address (for example street), I set it into Address list in client and update client, the address is not updated. I update the equlas method to compare street, city and other attributes of Address, but it doesn't help. So I see two ways: delete all Addresses from Client and add them with updated values, or update the Address separately outside the Client. Can toplink handle this without manuall coding? Here is my code: Code: // Client entity public class Client implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "idClient") private Integer idClient; @Column(name = "name") private String name; @Column(name = "surname") private String surname; @OneToMany(cascade = CascadeType.ALL, mappedBy = "clientidClient", fetch = FetchType.EAGER) private List<Address> addressList; ... // Address entity public class Address implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "idAddress") private Integer idAddress; @Column(name = "Street") private String street; @Column(name = "Number") private String number; @Column(name = "City") private String city; @Column(name = "Postcode") private Integer postcode; @Column(name = "Country") private String country; @JoinColumn(name = "Client_idClient", referencedColumnName = "idClient") @ManyToOne(cascade = CascadeType.ALL, optional = false) private Client clientidClient; ... |
| Free embeddable forum powered by Nabble | Forum Help |