我如何调整 Spring Data JDBCNamingStrategy使其行为像 Hibernate\xc2\xb4s PhysicalNamingStrategy?
\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 数据 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) 我想用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 开发一个新项目,因为它非常容易处理而且确实很棒。
在我的场景中,我有三种(将来可能更多)类型的projects. 因此,我的域模型可以使用类型继承轻松地使用普通的旧 java 对象进行建模。
第一个问题:
第二个问题 - 作为第一个问题的补充:
我得到了这个存储库代码:
\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);\nRun Code Online (Sandbox Code Playgroud)\n当我对该查询运行测试时,我收到此 Hibernate 警告:
\nHHH000104: firstResult/maxResults specified with collection …
我有这段代码:
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可能会发生以下步骤:
我真的能从这种情况中受益吗,因为我必须等待每种情况的结果(调用异步或同步的服务方法)?
使用它是好的做法吗Thread.sleep()?
我从这个例子中可以想象到的唯一好处是主线程可以自由(不被阻塞)用于其他计算(可能处理其他网络请求),而线程池线程执行耗时的过程?
我目前正在使用Spring Boot jar库中的可重用组件,例如
目的:
我们公司的每个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模块基础结构会更好吗?
使用 spring boot + spring data jdbc 我必须DataSource自己连接 bean。就像这样:
@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 }\nRun Code Online (Sandbox Code Playgroud)\n当我想使用 testcontainers 进行测试时,我DataSource还必须声明一个 bean:
@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 }\nRun Code Online (Sandbox Code Playgroud)\n使用我的测试,SpringBootTest我几乎必须使用ComponentScan所有包,因为我不想模拟所有其他 bean。
\n所以我的问题是:
\n我可以以某种方式排除主要DataSource …