小编Tho*_*ang的帖子

如何调整 Spring Data JDBC 的 NamingStrategy

我如何调整 Spring Data JDBCNamingStrategy使其行为像 Hibernate\xc2\xb4s PhysicalNamingStrategy
\n我有以下实体:

\n\n
/**\n * Campus domain model class.\n * Handles information about campus.\n *\n * @author thomas.lang@th-deg.de\n */\n@Data\n@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))\npublic class Campus {\n\n    private final @Id\n    @Wither\n    long campusId;\n\n    @NotNull\n    @Size(min = 3)\n    private String campusName;\n\n    /**\n     * Creates a new campus.\n     *\n     * @param campusName\n     */\n    public Campus(@NonNull String campusName) {\n        this.campusId = 0;\n        Assert.hasLength(campusName, "A valid value has to be provided for Campus!");\n        this.campusName = campusName;\n …
Run Code Online (Sandbox Code Playgroud)

spring-data spring-data-jdbc

7
推荐指数
1
解决办法
3023
查看次数

本地插入后如何获取新的记录ID

出于一些很好的原因,我做了一个 spring 数据 jpa 本机插入。
查询以正确的方式执行。
但我需要的是新生成的表 id 由nextval('hibernate_sequence')

有没有办法获得这个id?

这是我的查询:

/**
 * Inserts a new file attachment.
 * This is useful and maybe better suitable, because one may not want to load the whole
 * job offer to fullfill the JobOffer -> FileAttachment OneToMany Relation
 *
 * @param file       a given file
 * @param fileName   a given file name
 * @param jobOfferId a given job offer id
 * @return int the new id
 */
@Modifying
@Query(value = "insert …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa spring-data-jpa

5
推荐指数
1
解决办法
3861
查看次数

OneToMany Spring数据JDBC

我想用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)

java software-design spring-data spring-data-jdbc

5
推荐指数
1
解决办法
1560
查看次数

Spring Data JDBC是否支持继承

我正在使用 spring data jdbc 开发一个新项目,因为它非常容易处理而且确实很棒。

在我的场景中,我有三种(将来可能更多)类型的projects. 因此,我的域模型可以使用类型继承轻松地使用普通的旧 java 对象进行建模。

第一个问题:

  • 由于我使用的是 spring data jdbc,这种方式(继承)是否像 JPA 中那样支持?

第二个问题 - 作为第一个问题的补充:

  • 我在官方文档中找不到任何与此相关的内容。所以我假设有充分的理由不支持它。说到这里,我可能在一般情况下使用继承建模实体时走错了路吗?

spring-data-jdbc

5
推荐指数
1
解决办法
1603
查看次数

Spring Data JPA 分页 HHH000104

我得到了这个存储库代码:

\n
@Query(value = "select distinct r from Reference r " +\n        "inner join fetch r.persons " +\n        "left outer join fetch r.categories " +\n        "left outer join fetch r.keywords " +\n        "left outer join fetch r.parentReferences",\n        countQuery = "select count(distinct r.id) from Reference r " +\n                "inner join r.persons " +\n                "left outer join r.categories " +\n                "left outer join r.keywords " +\n                "left outer join r.parentReferences")\nPage<Reference> findsAllRelevantEntries(Pageable pageable);\n
Run Code Online (Sandbox Code Playgroud)\n

当我对该查询运行测试时,我收到此 Hibernate 警告:
\nHHH000104: firstResult/maxResults specified with collection …

hibernate jpa spring-data-jpa spring-boot

5
推荐指数
1
解决办法
4513
查看次数

Spring异步线程睡眠

我有这段代码:

final Future<ApplicationUser> user = applicationUserService.findIncludePrivilegesById(id);
    while (!user.isDone()) {
        Thread.sleep(100);
    }
    if (user.get() == null) throw new Exception(this.getMessageSource().getMessage("label.error.findError",
            null, locale));
    model.addAttribute("user", new ApplicationUserDto(user.get()));

    model.addAttribute("roles", Role.valuesMinusApi());
    model.addAttribute("groups", completeAuthorityList);
    model.addAttribute("message", this.getMessageSource().getMessage("label.dialog.userEdit", new Object[]{user.get().getLogin()}, locale));
    model.addAttribute("applicationUserStates", ApplicationUserState.values());
    return "administration/edit";
Run Code Online (Sandbox Code Playgroud)

转到applicationUserService.findIncludePrivilegesById(id)远程数据库服务器来查询数据。
我设计这段代码的想法是让这个(耗时的数据库通信)处理池中的空闲线程(通过使用异步)。

据我了解,async可能会发生以下步骤:

  1. 主线程进入方法
  2. 线程池中的线程查询数据
  3. 主线程等待或有剩余资源来做“其他”事情
  4. 如果线程池线程完成,我可以使用结果

我真的能从这种情况中受益吗,因为我必须等待每种情况的结果(调用异步或同步的服务方法)?
使用它是好的做法吗Thread.sleep()

我从这个例子中可以想象到的唯一好处是主线程可以自由(不被阻塞)用于其他计算(可能处理其他网络请求),而线程池线程执行耗时的过程?

java spring asynchronous spring-boot

4
推荐指数
1
解决办法
8566
查看次数

普通Java项目中Spring Boot库的用法

我目前正在使用Spring Boot jar库中的可重用组件,例如

  • ldap
  • 电子邮件
  • 用Apache Kafka进行消息传递
  • 其余api的用法

目的:
我们公司的每个Java“用户/编码器”都应该能够(通过maven或其他方式)将该jar放入一个项目中,并使用可重用的组件,而不是一遍又一遍地编码所有东西。

通过REST为该问题构建微服务不是我们的替代选择。

我的问题是:
我可以在任何普通的Java项目中重用这个Spring Boot jar库吗?

Beeing喜欢,我可以将这个jar库“放入”一个Java项目,并在我的“非Spring Boot”普通Java项目中将该库的Spring Boot服务连接起来吗?

注意/已编辑:
我已将Spring Boot用作项目模板(spring-boot-starter-parent)。
我像LdapTemplate手动配置模板一样,不要让Spring Boot发挥作用。

编辑
就Spring Boot / Spring项目中的重用而言,一切都很好。我已经做到了。

我对该库的目标可能是每个Java“用户”都可以使用该库,如下所示:
final SuperCoolLibary scl = new SuperCoolLibrary(); final boolean exists = scl.searchForLdapUser("tlang");

所以可能
会有另一个问题:将这个库切换到新的Java Jigsaw模块基础结构会更好吗?

java spring spring-boot

3
推荐指数
1
解决办法
141
查看次数

Spring Data JDBC 测试容器数据源

使用 spring boot + spring data jdbc 我必须DataSource自己连接 bean。就像这样:

\n
@Bean\n    public DataSource dataSource() {\n        HikariConfig hikariConfig = new HikariConfig();\n        hikariConfig.setJdbcUrl(this.environment.getRequiredProperty("url"));\n        hikariConfig.setUsername("user");\n        hikariConfig.setDriverClassName("org.postgresql.Driver");\n        hikariConfig.setPassword("password");\n        return new HikariDataSource(hikariConfig);\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

当我想使用 testcontainers 进行测试时,我DataSource还必须声明一个 bean:

\n
@Bean\n        @Primary\n        DataSource testDataSource() {\n\n            if (POSTGRESQL_CONTAINER == null) {\n                PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("9.6.12"));\n                container.withInitScript("schema.sql");\n                container.start();\n                POSTGRESQL_CONTAINER = container;\n            }\n            PGSimpleDataSource dataSource = new PGSimpleDataSource();\n            dataSource.setUrl(POSTGRESQL_CONTAINER.getJdbcUrl());\n            dataSource.setUser(POSTGRESQL_CONTAINER.getUsername());\n            dataSource.setPassword(POSTGRESQL_CONTAINER.getPassword());\n\n            return dataSource;\n        }\n
Run Code Online (Sandbox Code Playgroud)\n

使用我的测试,SpringBootTest我几乎必须使用ComponentScan所有包,因为我不想模拟所有其他 bean。
\n所以我的问题是:
\n我可以以某种方式排除主要DataSource …

spring-boot testcontainers spring-data-jdbc

3
推荐指数
1
解决办法
3368
查看次数