如何将两个表记录映射到 java 个对象的列表
How to map two tables records to list of java objects
我在 postgresql 数据库中有两个 table 的作者和博客,我想检索记录并 map/bind 使用简单的 JDBC 客户端列出对象,下面是 table 和对象详细信息
架构详细信息
author (TABLE)
id VARCHAR(10)
name VARCHAR(200)
dob VARCHAR(200)
blogsv (TABLE)
id VARCHAR(10)
author VARCHAR(10)
subject VARCHAR(200)
content VARCHAR(200)
作者table
博客table
public class Author{
private String id;
private String name;
private Date dob;
private List<Blogs> blogs; // Bind related blog records
}
public class Blog{
private String id;
private String authorId;
private String subject;
private Date content;
}
我目前正在获取所有作者记录并首先创建作者对象,然后通过迭代先前创建的作者对象来查询博客,并创建将其映射到作者的博客对象。有什么有效的方法吗?
我无法调整图片大小,因此占用了大部分空间。
这是一个可能对设计代码有用的指南。
- 在数据库级别
a) blogsv
,列 author
应该是 int
并且是 author
的外键
(FK
需标在create-table
上)
即使 varchar
也能正常工作,但一致设计的数据库规则已被破坏
b) 最好使用一个带有连接的查询来一次检索所有日期
select author.id, author.name, author.dob,
blogsv.id blogId,blogsv.subject, blogsv.content
from author left join blogsv on author.id = blogsv.author
- 在Java侧
a) 在映射中使用对应类型(sql-列到 java)
public class Author{
private int id;
private String name;
private Date dob;
//could also create by default an empty list for each author;
private List<Blogs> blogs = new ArrayList<Blogs>();
// Bind related blog records
}
public class Blog{
private int id;
//do not need since it's on top level (nested with author)
//private String authorId;
private String subject;
private Date content;
}
b) 增强逻辑
//better to use a map for easy access to author by id
Map<Integer, Author> authorMap = new HashMap<>();
//after retrieve data from sql
//loop each record
//check if author is key on Map
//NO: create new author
authorMap.put(id, new Author(...))
//YES: retrieve author
Author a = authorMap.get(id)
//add blog detail if any (fields are not null)
//need also to retrieve from java associate blogList
a.getBlogList().add(blogDetails)
//end loop
注意:id
被定义为 varchar
直到一个点,但主要 PK
应该是 integer
并且在大多数情况下 autoincrement
JPA 提供程序
在长 运行 中,使用具有相应实体的 JPA 提供程序将是有益的
- 将 SQL tables/relations 映射到 Java 个实体
- 使用 JPA 提供程序管理事务、CRUD 操作和准备好的语句(以防止 SQL 注入攻击)
如何
- 根据作者和博客sqltable结构创建
javax.persistence.
实体(@Entity
)
- 在
Author
中为 List<Blog> blogs
定义 @OneToMany(mappedBy = "author")
- 在
Blog
中为 private Author author
定义 @ManyToOne @JoinColumn(name="author")
我在 postgresql 数据库中有两个 table 的作者和博客,我想检索记录并 map/bind 使用简单的 JDBC 客户端列出对象,下面是 table 和对象详细信息
架构详细信息
author (TABLE)
id VARCHAR(10)
name VARCHAR(200)
dob VARCHAR(200)
blogsv (TABLE)
id VARCHAR(10)
author VARCHAR(10)
subject VARCHAR(200)
content VARCHAR(200)
作者table
博客table
public class Author{
private String id;
private String name;
private Date dob;
private List<Blogs> blogs; // Bind related blog records
}
public class Blog{
private String id;
private String authorId;
private String subject;
private Date content;
}
我目前正在获取所有作者记录并首先创建作者对象,然后通过迭代先前创建的作者对象来查询博客,并创建将其映射到作者的博客对象。有什么有效的方法吗?
我无法调整图片大小,因此占用了大部分空间。
这是一个可能对设计代码有用的指南。
- 在数据库级别
a)blogsv
,列author
应该是int
并且是author
的外键
(FK
需标在create-table
上)
即使varchar
也能正常工作,但一致设计的数据库规则已被破坏
b) 最好使用一个带有连接的查询来一次检索所有日期
select author.id, author.name, author.dob,
blogsv.id blogId,blogsv.subject, blogsv.content
from author left join blogsv on author.id = blogsv.author
- 在Java侧
a) 在映射中使用对应类型(sql-列到 java)
public class Author{
private int id;
private String name;
private Date dob;
//could also create by default an empty list for each author;
private List<Blogs> blogs = new ArrayList<Blogs>();
// Bind related blog records
}
public class Blog{
private int id;
//do not need since it's on top level (nested with author)
//private String authorId;
private String subject;
private Date content;
}
b) 增强逻辑
//better to use a map for easy access to author by id
Map<Integer, Author> authorMap = new HashMap<>();
//after retrieve data from sql
//loop each record
//check if author is key on Map
//NO: create new author
authorMap.put(id, new Author(...))
//YES: retrieve author
Author a = authorMap.get(id)
//add blog detail if any (fields are not null)
//need also to retrieve from java associate blogList
a.getBlogList().add(blogDetails)
//end loop
注意:id
被定义为 varchar
直到一个点,但主要 PK
应该是 integer
并且在大多数情况下 autoincrement
JPA 提供程序
在长 运行 中,使用具有相应实体的 JPA 提供程序将是有益的
- 将 SQL tables/relations 映射到 Java 个实体
- 使用 JPA 提供程序管理事务、CRUD 操作和准备好的语句(以防止 SQL 注入攻击)
如何
- 根据作者和博客sqltable结构创建
javax.persistence.
实体(@Entity
) - 在
Author
中为List<Blog> blogs
定义 - 在
Blog
中为private Author author
定义
@OneToMany(mappedBy = "author")
@ManyToOne @JoinColumn(name="author")