我正在尝试构建一个select语句,这将允许我在应用程序启动时构建一个本地应用程序缓存。我的表结构如下所示;
我知道要得到一些帮助,我必须证明我已经尝试过但非常
我正在使用hibernate,它应该让事情变得更容易但是我真的遇到了障碍,我能想到的唯一方法是选择所有字段,然后在表id匹配上做左外连接,但问题是使用这种方法的是,一个用户可以拥有0-1个名片/ 0-1个社交卡,0-1个人卡,这意味着当我离开外部联接时,某些用户可能会丢失一些数据。 personalInfo / Logindetails和Device Data都是1:1映射
有没有更有效的方法在hibernate中进行select,这样我可以轻松构建缓存?
理想情况下,我想构建一个这样的对象;
public class User {
private BusinessCard businessCard;
private SocialCard socialCard;
private PersonalCard personalCard;
private PersonalInformation personalInformation;
private LoginDetails loginDetails;
private DeviceData deviceData;
public BusinessCard getBusinessCard() {
return businessCard;
}
public void setBusinessCard(BusinessCard businessCard) {
this.businessCard = businessCard;
}
public SocialCard getSocialCard() {
return socialCard;
}
public void setSocialCard(SocialCard socialCard) {
this.socialCard = socialCard;
}
public PersonalCard getPersonalCard() {
return personalCard;
}
public void setPersonalCard(PersonalCard personalCard) {
this.personalCard = personalCard;
}
public PersonalInformation getPersonalInformation() {
return personalInformation;
}
public void setPersonalInformation(PersonalInformation personalInformation) {
this.personalInformation = personalInformation;
}
public LoginDetails getLoginDetails() {
return loginDetails;
}
public void setLoginDetails(LoginDetails loginDetails) {
this.loginDetails = loginDetails;
}
public DeviceData getDeviceData() {
return deviceData;
}
public void setDeviceData(DeviceData deviceData) {
this.deviceData = deviceData;
}
}
由于
答案 0 :(得分:0)
来自Hibernate Community Documentation - Charpter 7 - Association Mappings:
<强> 7.2.1。许多对一强>
单向多对一关联是最常见的单向关联。
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person (
personId bigint not null primary key
, addressId bigint not null
)
create table Address (
addressId bigint not null primary key
)
<强> 7.2.2。一对一的强>
外键上的单向一对一关联几乎相同。唯一的区别是列唯一约束。
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person (
personId bigint not null primary key
, addressId bigint not null unique
)
create table Address (
addressId bigint not null primary key
)
这是你如何映射一对一的关系。请注意,除了many-to-one
。
unique constraint
映射相同
现在已经映射了您的班级,您的Person
班级将如下所示。
public class Person {
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
public int getId() { return id; }
protected void setId(int id) { this.id = id; }
private Address address;
private int id;
}
回答您的问题
这完全取决于您希望如何保留数据。如上所述,我会将BusinessCard
,SocialCard
和PersonalCard
的身份放入User
表中,以便每个用户可以存在一张且只有一张卡。
<class name "User" table="Users">
<id name="id" column="userId">
<generator class="native" />
</id>
<many-to-one name="businessCard"
column="businessCardId"
unique="true"
not-null="true" />
<many-to-one name="socialCard"
column="socialCardId"
unique="true"
not-null="true" />
<many-to-one name="personalCard"
column="personalCardId"
unqiue="true"
not-null="true" />
</class>
<class name="BusinessCard" table="BusinessCards">
<id name="id" column="businessCardId">
<generator class="native" />
</id>
</class>
<class name="SocialCard" table="SocialCards">
<id name="id" column="socialCardId">
<generator class="native" />
</id>
</class>
<class name="PersonalCard" table="PersonalCards">
<id name="id" column="personalCardId">
<generator class="native" />
</id>
</class>