java ORMLite OneToOne关系
我得到了下一个使用OneToOne关系的数据库结构:
[公司]
公司id(PK)
公司名称
[公司配置]
公司配置id(自动增量,主键)
公司id(UNIQUEKEY,FK)
公司配置
我一直在使用ORMlite,接下来我将为这两个表学习以下课程:
@DatabaseTable(tableName = "company")
public class Company {
public static final String ID_COMPANY = "company_id";
public static final String COMPANY_NAME = "company_name";
@DatabaseField(generatedId = true, columnName = ID_COMPANY)
private int idCompany;
@DatabaseField(columnName = COMPANY_NAME)
private String companyName;
@DatabaseTable(tableName = "company_configuration")
public class CompanyConfiguration {
public static final String COMPANY_CONFIGURATION_ID = "company_configuration_id";
public static final String COMPANY_ID = "company_id";
public static final String COMPANY_CONFIGURATION_V = "company_configuration_v";
@DatabaseField(generatedId = true, columnName = COMPANY_CONFIGURATION_ID)
private int idCompanyConfiguration;
@DatabaseField(foreign = true,foreignAutoRefresh = true, columnName = COMPANY_ID)
private Company companyId;
@DatabaseField(columnName = COMPANY_CONFIGURATION_V)
private String companyConfigurationV;
这里是OneToOne关系,因为我想划分一个包含许多列的表。
正如您在上面的示例中所看到的,从Company
类到CompanyConfiguration
类之间没有关系
我知道我可以将这段代码(下面的示例)添加到Company
类中,但我不需要@ForeignCollectionField,因为集合将只包含一个CompanyConfiguration对象:
@ForeignCollectionField()
private ForeignCollection<CompanyConfiguration> companyConfigurations;
我需要在Company
类中添加类似的内容(如下示例),并将从Company
类获得对CompanyConfiguration
类的引用:
@OneToOne(targetEntity = CompanyDbConfig.class)
@JoinTable(name = "company_configuration")
@JoinColumn(name = "id_company")
CompanyConfiguration companyConfiguration;
很快,我想使用ORMlite获取公司对象。请参见下面的示例。从数据库中获取company后,我希望在company对象中包含和CompanyConfiguration对象
Company company = daoCompany.queryForId(id); //daoCompany is an instance of ORMlite Dao class
有可能吗?如何使用ORMlite实现
# 1 楼答案
我自己发了一个奥姆利特问题,所以我查看了所有未回答的问题,看看是否有什么我能回答的。尽管这是一个古老的话题,但我想尝试一下,以防它能帮助别人
我看过你的帖子好几次了,我想你是在问如何将两个表中的信息加载到一个模型中。在数据库中将一个相当大的表拆分为两个,但希望它作为一个模型返回。如果这是正确的,下面是我对代码的看法。这假设您希望使用对象来构建查询,而不是传入查询字符串
你会保留现有的模型,这样它们就可以用作DTO。你只需要使用上面的新模型来传回你想要的确切属性
*我是用Notepad++写的,请原谅我的拼写错误