問題は、 anuser
には 1 つ以上properties
の があるため、これが私のエンティティ クラスです。user
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
// others attributes
//bi-directional many-to-one association to Property
@OneToMany(mappedBy="user", fetch=FetchType.EAGER)
private List<Property> properties;
..
}
私のエンティティproperty
:
@Entity
public class Property implements Serializable {
private static final long serialVersionUID = 1L;
..
//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="id_user")
private User user;
..
}
これらのエンティティは によって生成されましたEclipseLink
。
そして、これが私のテーブルです。これは、私が望むものには問題ないと思います。
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
..
PRIMARY KEY (`id`),
UNIQUE KEY `email_UNIQUE` (`email`),
KEY `u_id_city_to_ac_id_idx` (`id_city`),
KEY `u_id_state_to_as_id_idx` (`id_state`),
KEY `u_id_country_to_acy_id_idx` (`id_country`),
CONSTRAINT `u_id_city_to_ac_id` FOREIGN KEY (`id_city`) REFERENCES `address_city` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `u_id_country_to_acy_id` FOREIGN KEY (`id_country`) REFERENCES `address_country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `u_id_state_to_as_id` FOREIGN KEY (`id_state`) REFERENCES `address_state` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8 COMMENT='an user can be a tenant and a landlord also';
そして私のproperty
テーブル:
CREATE TABLE `property` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_user` int(11) unsigned NOT NULL,
..
PRIMARY KEY (`id`),
KEY `p_id_accomodation_to_pac_id_idx` (`id_accomodation`),
KEY `p_id_room_type_to_prt_id_idx` (`id_room_type`),
KEY `p_id_bed_type_to_pbt_id_idx` (`id_bed_type`),
KEY `p_id_policty_cancelation_to_pc_id_idx` (`id_policy_cancelation`),
KEY `p_id_user_to_u_id_idx` (`id_user`),
KEY `p_id_city_to_ac_id_idx` (`id_city`),
KEY `p_id_state_to_as_id_idx` (`id_state`),
KEY `p_id_country_to_acy_id_idx` (`id_country`),
CONSTRAINT `p_id_accomodation_to_pac_id` FOREIGN KEY (`id_accomodation`) REFERENCES `property_accomodation` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `p_id_bed_type_to_pbt_id` FOREIGN KEY (`id_bed_type`) REFERENCES `property_bed_type` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `p_id_city_to_ac_id` FOREIGN KEY (`id_city`) REFERENCES `address_city` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `p_id_country_to_acy_id` FOREIGN KEY (`id_country`) REFERENCES `address_country` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `p_id_policty_cancelation_to_pc_id` FOREIGN KEY (`id_policy_cancelation`) REFERENCES `policy_cancellation` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `p_id_room_type_to_prt_id` FOREIGN KEY (`id_room_type`) REFERENCES `property_room_type` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `p_id_state_to_as_id` FOREIGN KEY (`id_state`) REFERENCES `address_state` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `p_id_user_to_u_id` FOREIGN KEY (`id_user`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
たとえば、ログインアクションですべてproperties
を取得したいと思います。user
user = userEAO.find(user.getEmail());
user.getProperties(); //always empty
どうやってやるの ?どのタイプを入れればいいのかと思ったのfetching
ですが、そうではないようです。
更新LAZY
してmy
に変更しFetchType
ます。今見てください:
// プロパティ @OneToMany(mappedBy="user", fetch=FetchType.LAZY) プライベート リスト プロパティへの双方向の多対 1 関連付け。
システムにuser
ログインすると:
User u = userEAO.findByEmail(profile.getEmail());
if (u != null){
if (u.getProperties() != null){
u.getProperties().size();
System.out.println(u.getProperties().size()); // now prints 1
}
ジェームス、オブジェクトを新しく作成するとき、property
これuser
はあなたが意味するものですよね?はい、これを作成した後、リストに追加します=]refreshing
property
property
user
properties
public Message create(Property property) {
try{
em.persist(property);
em.flush();
em.refresh(property);
}catch(ConstraintViolationException cve){
cve.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
// 1062 : duplicate entry
if (e.getMessage().contains("Duplicate"))
return new Message(-1000);
else{
e.printStackTrace();
return new Message(-1);
}
}
return new Message(0);
}