我想了解为什么 spring-data-jdbc 提供与 MyBatis 的集成。
我尝试在 RDBMS 中创建用户/角色关系,并希望使用 R2dbc(Spring Data R2dbc) 与后端数据库握手。
假设有三个表,users、roles 和user_roles。
@Table("users")
class User {
@Id
private String username;
private String password;
private String email;
@Builder.Default
private boolean active = true;
@Builder.Default
private List<String> roles = new ArrayList<>();
@Column("created_at")
private LocalDateTime createdDate;
}
Run Code Online (Sandbox Code Playgroud)
与 JPA 不同,R2dbc 重用 spring-data-relational-common(在 Spring Data Jdbc 中也使用)来注释表,但没有解决关系的工具,例如roles此处。
postgresql spring-boot spring-data-jdbc spring-data-r2dbc r2dbc
我正在尝试使用 Spring Data JDBC 和 Spring Boot 连接到 Firebird 数据库。我使用 Spring Tools 创建了一个简单的应用程序。Spring Data JDBC 无法识别方言。我认为问题在于DialectResolver它不支持 Firebird。
private static Dialect getDialect(Connection connection) throws SQLException {
DatabaseMetaData metaData = connection.getMetaData();
String name = metaData.getDatabaseProductName().toLowerCase(Locale.ENGLISH);
if (name.contains("hsql")) {
return HsqlDbDialect.INSTANCE;
}
if (name.contains("h2")) {
return H2Dialect.INSTANCE;
}
if (name.contains("mysql")) { // catches also mariadb
return new MySqlDialect(getIdentifierProcessing(metaData));
}
if (name.contains("postgresql")) {
return PostgresDialect.INSTANCE;
}
if (name.contains("microsoft")) {
return SqlServerDialect.INSTANCE;
}
if (name.contains("db2")) {
return Db2Dialect.INSTANCE;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我是 …
我尝试将以下内容与 spring-data-jdbc 和 postgres 驱动程序(kotlin)一起使用,
data class MyEntity(
val id: UUID,
val content: String
)
Run Code Online (Sandbox Code Playgroud)
使用字符串失败并出现以下错误,
org.postgresql.util.PSQLException: ERROR: column "content" is of type jsonb but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 31
Run Code Online (Sandbox Code Playgroud)
我不确定如何使用 String -> jsonb 转换器
在我的项目中,我使用 spring-data-jdbc 版本 2.0.4.RELEASE,但我遇到了如何为表指定架构的问题。实体类看起来像这样
@Data
@Table(value = "alpha.op_organization")
public class OrganizationEntity {
@Id
private Long id;
@Column(name="name")
private String name;
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用 jdbc 存储库方法保存实体,save如下所示
OrganizationEntity organization = new OrganizationEntity();
organization.setName("OrgName1");
organizationRepository.save(organisation)
Run Code Online (Sandbox Code Playgroud)
我遇到异常“关系 alpha.op_organization 不存在。相应的 sql 查询是
INSERT INTO "alpha.op_organization" ("name") VALUES (?)
Run Code Online (Sandbox Code Playgroud)
IE。事实证明,我们正在尝试在表“alpha.op_organization”中插入条目,而不是在模式 alpha 的表 op_organization 中插入条目。
我尝试自定义 NamingStrategy 以返回模式 alpha,但没有成功。
@Configuration
public class AppConfig extends AbstractJdbcConfiguration {
@Bean
public NamingStrategy namingStrategy() {
return new NamingStrategy() {
@Override
public String getSchema() {
return "alpha";
}
};
}
} …Run Code Online (Sandbox Code Playgroud) 我正在探索在使用 Spring Data R2DBC 时设计一对一和一对多关系时可能的想法。
由于 Spring Data R2DBC 本身仍然不支持关系,因此仍然需要我们自己处理这些关系(与 Spring Data JDBC 不同)。
我想象的是,当涉及到一对一映射时,实现可能如下所示:
@Table("account")
public class Account {
@Id
private Long id;
@Transient // one-to-one
private Address address;
}
Run Code Online (Sandbox Code Playgroud)
@Table("address")
public class Address {
@Id
private Integer id;
}
Run Code Online (Sandbox Code Playgroud)
而数据库模式定义如下:
--address
CREATE TABLE address
(
id SERIAL PRIMARY KEY
)
--account
CREATE TABLE account
(
id SERIAL PRIMARY KEY,
address_id INTEGER REFERENCES address(id)
)
Run Code Online (Sandbox Code Playgroud)
由于该Account对象是我的聚合根,我想我应该Address按照 Jens Schaduer 的建议加载该对象:
聚合是形成一个单元的一组对象,它应该始终保持一致。此外,它应该始终被持久化(并加载)在一起。来源:Spring Data JDBC、参考资料和聚合
这让我想到,在像这样的一对一关系的情况下,我实际上应该Account …
我想用Spring Data JDBC建模OneToMany Relation.我在这个非常有用的博客上阅读了https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates,当你想为ToMany参考建模时你应该使用引用:
因此,任何"多对一"和"多对多"关系都必须通过引用id来建模.
所以我有这样的场景:
一个Student可以有多个Registration.一个人Registration可以只有一个Student.如果删除Registration分配的Student不应该删除级联.
我最终得到了这个建模:
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class Registration {
private final @Id
@Wither
long registrationId;
@NotNull
private String electiveType;
@NotNull
private LocalDateTime created = LocalDateTime.now();
@NotNull
private StudentRegistrationReference studentRegistrationReference;
}
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class StudentRegistrationReference {
private long student;
private long registration;
}
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Spring Data JDBC 用于我的 PostgreSQL 数据库。我定义了以下bean
@Data
class Report {
@Id
private Long id;
private String name;
private Set<Dimension> dimensions;
}
@Data
class Dimension {
private String name;
private Long[] filterIds;
}
Run Code Online (Sandbox Code Playgroud)
以及相应的DDL
CREATE TABLE report (
id bigserial PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE dimension (
id bigserial PRIMARY KEY ,
report bigint,
name text,
filter_ids bigint[],
FOREIGN KEY (report) REFERENCES report(id) ON DELETE CASCADE ON UPDATE CASCADE
);
Run Code Online (Sandbox Code Playgroud)
然后我尝试插入报告
final Dimension dimension = new …Run Code Online (Sandbox Code Playgroud) 我正在使用 spring data jdbc 开发一个新项目,因为它非常容易处理而且确实很棒。
在我的场景中,我有三种(将来可能更多)类型的projects. 因此,我的域模型可以使用类型继承轻松地使用普通的旧 java 对象进行建模。
第一个问题:
第二个问题 - 作为第一个问题的补充:
我尝试@CreatedDate与 spring-data-jdbc 和 PostgreSQL 数据库结合使用来跟踪首次创建表条目的时间。这在插入新行时有效(它正确地将其设置为当前日期),但在更新条目时,我收到异常,因为它被设置为空,这是我的配置不允许的。
这是我的实体(省略了其他不相关的字段):
@Table("my_entity")
public class MyEntity {
@Id
private final Long id;
@Column("created_at")
@CreatedDate
private Instant createdAt;
public MyEntity (Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public Instant getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的存储库:
public interface MyRepository extends CrudRepository<MyEntity, Long> {}
Run Code Online (Sandbox Code Playgroud)
这是相关配置:
@Configuration
@EnableJdbcRepositories("com.example")
@EnableJdbcAuditing
public class JdbcConfig extends AbstractJdbcConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
使用该repository.saveAll()方法,createdAt第一次就可以正确设置该字段。当使用相同的方法更新现有实体时,当我希望跳过该字段时,它被设置为 …
spring-data-jdbc ×10
spring-boot ×5
spring-data ×4
java ×3
postgresql ×2
r2dbc ×2
firebird ×1
jaybird ×1
jdbc ×1
mybatis ×1
spring ×1