从 JPA 文档中我可以看到 AUTO 是默认的刷新模式,刷新应该在任何查询执行之前发生。我已经在 spring boot jpa 上尝试过这个,我可以看到来自不同实体的查询不会发生刷新,这是预期的吗?即使不同的实体可能与其有关系(此处为“部门 <--> 人员”)
根据本文,刷新应在任何查询之前触发: https://vladmihalcea.com/how-do-jpa-and-hibernate-define-the-auto-flush-mode/
// this triggers flush //
Person person = personRepository.findById(5L).get();
person.setName("hello test");
Person person1 = (Person) entityManager.createQuery("select person from Person
person where person.id=11").getSingleResult(); // flush before query
// this doesn't trigger flush even if the department has the person //
Person person = personRepository.findById(5L).get();
person.setName("hello test");
Department department= (Department) entityManager.createQuery("select
department from Department
department where department.id=1").getSingleResult();
Run Code Online (Sandbox Code Playgroud)
更新:
我注意到刷新只发生在具有 DML 的同一张表上的 JPQL 查询上,而对于本机 sql 查询,如果之前有 DML ,它将始终在任何查询之前刷新。即使没有发生刷新,JPQL 也会返回经过修改的托管实体,而不是数据库中的实体。谁能解释一下这是否符合 JPA …
我在看流界面,发现这个方法:
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Run Code Online (Sandbox Code Playgroud)
我找不到任何理由为什么"?延伸R"代替R:
<R> Stream<R> map(Function<? super T, R> mapper);
Run Code Online (Sandbox Code Playgroud)
那么如果我像上面这样做会有什么不同?不是"?" 是我传递的R变量?它会延长RI无法找到原因.
我正在使用Spring版本4(spring数据),我想将Object作为JSON返回,我想知道即使没有使用xmlRootElement注释用户类,下面的代码也能正常工作:
@RequestMapping(value = "/resources/users", method = RequestMethod.GET)
public ResponseEntity<User> getUserByLogonId(OAuth2Authentication auth) {
String userLogonId = ((org.springframework.security.core.userdetails.User) auth.getUserAuthentication()
.getPrincipal()).getUsername();
UsersServices usersServices = new UsersServicesImpl(usersOperations);
User user = usersServices.findByLogonId(userLogonId);
HttpStatus userStatus = HttpStatus.NOT_FOUND;
if (user != null) {
userStatus = HttpStatus.FOUND;
}
return new ResponseEntity<User>(user, userStatus);
}
Run Code Online (Sandbox Code Playgroud)
任何机构都能解释一下 是ResponseBody/ResponseEntity自己做的工作?当我需要注释要作为JSON返回的对象类.
我已按照指南将 Spring Boot 配置为与 tomcat 一起使用:https : //www.baeldung.com/spring-boot-war-tomcat-deploy。当我从 IDE 运行应用程序时,我可以在控制台上看到所有内容,但在 tomcat 上,日志没有显示任何内容,我还配置了以下内容:
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
Run Code Online (Sandbox Code Playgroud)
没有运气任何想法?
我的 application.properites
# ===============================
# = DATA SOURCE
# ===============================
spring.datasource.url = jdbc:mysql://...
spring.datasource.username = root
spring.datasource.password = admin
# ===============================
# = JPA / HIBERNATE
# ===============================
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
# ==============================================================
# = Spring Security / Queries for AuthenticationManagerBuilder
# ==============================================================
spring.queries.users-query=select email, password, active from user where email=?
spring.queries.roles-query=select u.email, r.role from user u …Run Code Online (Sandbox Code Playgroud) 我试图从文档中了解空 bean 失败何时发生:
public static final SerializationFeature FAIL_ON_EMPTY_BEANS 确定在找不到类型的访问器时会发生什么的功能(并且没有注释表明它是要序列化的)。如果启用(默认),则抛出异常以指示这些为不可序列化的类型;如果禁用,它们将被序列化为空对象,即没有任何属性。请注意,此功能仅对那些没有任何可识别注释(如@JsonSerialize)的“空”bean 起作用的空类型:具有注释的那些不会导致抛出异常。
默认情况下启用该功能。
现在我尝试创建一个空的 POJO(没有 getter):
@Component
public class Wrapper2 {
private String name;
}
Run Code Online (Sandbox Code Playgroud)
我要序列化的包装类:
@Component
public class Wrapper {
@Autowired
private Wrapper2 wrapper2;
}
Run Code Online (Sandbox Code Playgroud)
控制器:
@RestController
public class TestController {
@Autowired
private Wrapper wrapper;
@GetMapping("/test")
public Wrapper test() {
return wrapper;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,即使添加/删除 wrapper2 的 getter 和 setter,我也总是将包装器序列化。谁能解释什么时候会发生错误?
我在这里读到这个问题:Java:静态最终字段以什么顺序初始化?
根据答案
“除了最终类变量和值为编译时常量的接口字段首先被初始化......”
我认为这是不正确的,因为以下操作会失败:
static {
String y = x;
}
public static final String x = "test";
Run Code Online (Sandbox Code Playgroud)
在静态块中,x不被识别。如果这个答案是正确的,有人可以发表评论吗?
我在与配置类不同的包中有一个存储库,因此我使用@Repostiory 将其注释为以下内容:
package test;
@Repository
public interface UserTest extends JpaRepository<User, Long> {
}
Run Code Online (Sandbox Code Playgroud)
我已经对其进行了组件扫描,但没有奏效:
package com.app;
@SpringBootApplication
@ComponentScan({"test","com.app"})
public class Application extends SpringBootServletInitializer {
}
Run Code Online (Sandbox Code Playgroud)
例外:没有可用的“test.UserTest”类型的合格 bean:预期至少有 1 个 bean 有资格作为自动装配候选。
除非我添加 enableJpaRepositories ,否则为什么组件扫描不能在存储库上工作?我认为 componetScan 就足够了
更新:
由于某些答案提供了解决方案,我问的是解释而不是解决方案。以下内容甚至无需对“test”进行组件扫描即可工作:
SpringBootApplication
@EnableJpaRepositories({"test","com.app"})
public class Application extends SpringBootServletInitializer{
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是为什么我什至需要在 @Repository 上使用 componentscan 不起作用时?为什么在文档中@Repository 被componentscan 扫描时它没有效果并且@EnableJpaRepostiories 是enoguh?
来自关于组件扫描的 Spring 文档:指示是否应启用对使用 @Component @Repository、@Service 或 @Controller 注释的类的自动检测。
没有检测到我的@Repository
我正在使用 spring boot 构建一个胖罐子,该胖罐子使用
java -jar app.jar
Run Code Online (Sandbox Code Playgroud)
问题是我正在使用 docker 并且我想扩展 jar 内容以获得更好的可用性,我执行以下操作来提取:
unzip app.jar
Run Code Online (Sandbox Code Playgroud)
现在我用以下命令运行罐子:
java -cp "." org/springframework/boot/loader/JarLauncher
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jaxRsServer' defined in class path resource
Run Code Online (Sandbox Code Playgroud)
所以它无法找到我的bean,尽管它已配置:
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@ImportResource("classpath:spring/beans_context.xml")
public class SpringBootJaxrsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJaxrsApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
beans_context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<import resource="classpath:META-INF/spring/*_context.xml"/>
Run Code Online (Sandbox Code Playgroud) 我试图在 sed -e 命令中使用 $ 并且它有效,例如:
sed -e 's/world$/test/g' test.txt
Run Code Online (Sandbox Code Playgroud)
上面的命令将替换字符串末尾的“world”。
令我困惑的是,以下内容确实有效:
sed -e 's/${projects.version}/20.0/g' test.txt
Run Code Online (Sandbox Code Playgroud)
上面的命令替换了${projects.version},我没有任何解释 sed 是如何匹配的$并且没想到它是一个特殊字符?
我正在使用带有JPA(休眠)的spring boot,在我转向spring boot之前我使用Spring数据JPA和hibernate,默认是热切地加载属性值并懒惰地加载集合.
在春季启动JPA以下是默认情况下急切提取的原因?角色将在用户内部返回,但它们应为null
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
Run Code Online (Sandbox Code Playgroud)
这就是我遵循的例子https://medium.com/@gustavo.ponce.ch/spring-boot-spring-mvc-spring-security-mysql-a5d8545d837d
我编辑的application.properties如下:
# ===============================
# = JPA / HIBERNATE
# ===============================
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
# ==============================================================
# = Spring Security / Queries for AuthenticationManagerBuilder
# ==============================================================
spring.queries.users-query=select email, password, active from user where email=?
spring.queries.roles-query=select u.email, r.role from user u inner join user_role ur on(u.id=ur.user_id) inner join role r on(ur.role_id=r.id) …Run Code Online (Sandbox Code Playgroud) java ×8
spring-boot ×6
spring ×5
jpa ×2
bash ×1
flush ×1
hibernate ×1
jackson ×1
java-stream ×1
linux ×1
regex ×1
sed ×1
shell ×1
spring-data ×1
spring-mvc ×1
static ×1
tomcat ×1
webapp2 ×1