问题是,一个user
有一个或多个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
我的FetchType
,现在看:
//与属性的双向多对一关联 @OneToMany(mappedBy="user", fetch=FetchType.LAZY) 私有列表属性;
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);
}