我正在尝试将 JPA@Embeddable与 Hibernate一起使用。实体和可嵌入对象都有一个名为 的属性id:
@MappedSuperclass
public abstract class A {
@Id
@GeneratedValue
long id;
}
@Embeddable
public class B extends A {
}
@Entity
public class C extends A {
B b;
}
Run Code Online (Sandbox Code Playgroud)
这引发了一个org.hibernate.MappingException: component property not found: id.
我想避免使用@AttributeOverrides. 因此我尝试设置spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.DefaultComponentSafeNamingStrategy(我使用的是 Spring Boot)。这没有任何影响(相同的例外)。但是,我怀疑该设置被忽略了,因为指定一个不存在的类不会引发异常。
奇怪的是,即使有这个变体
@Entity
public class C extends A {
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="id", column = @Column(name="b_id") ),
} )
B b;
}
Run Code Online (Sandbox Code Playgroud)
我仍然遇到同样的错误。
我正在尝试使用 Thin 和 nginx 作为反向代理来运行 Redmine。
我的/etc/thin2.1/redmine.yml:
---
pid: /var/run/thin/redmine.pid
group: redmine
wait: 30
timeout: 30
log: /var/log/thin/redmine.log
max_conns: 1024
require: []
environment: production
max_persistent_conns: 512
servers: 4
daemonize: true
user: redmine
socket: /var/run/thin/redmine.sock
chdir: /var/www/projects.mydomain.tld
Run Code Online (Sandbox Code Playgroud)
当我使用网络浏览器访问该网站时,我会得到一个502 Bad Gateway. 这是以下内容/var/log/thin/redmine.0.log:
>> Writing PID to /var/run/thin/redmine.0.pid
>> Changing process privilege to redmine:redmine
>> Using rack adapter
>> Exiting!
/usr/lib/ruby/vendor_ruby/thin/daemonizing.rb:158:in `delete': Permission denied @ unlink_internal - /var/run/thin/redmine.0.pid (Errno::EACCES)
from /usr/lib/ruby/vendor_ruby/thin/daemonizing.rb:158:in `remove_pid_file'
from /usr/lib/ruby/vendor_ruby/thin/daemonizing.rb:59:in `block in daemonize' …Run Code Online (Sandbox Code Playgroud) 我在应用程序中使用 Spring JPA 存储库和实体。现在,在该应用程序的一种风格中,我需要扩展我的一个实体并提供一个扩展的存储库。
对于我需要覆盖/扩展的所有其他 bean,我只需创建一个新实现并使用 @Primary 对其进行注释,这样它将被自动装配而不是默认实现。
但是,对于存储库,这不起作用。我可以用@Primary 注释新的存储库,但它没有效果(两个 bean 都被找到,因此不能自动装配)。这是有道理的,因为存储库是一个接口而不是一个实现,实现是由 Spring 动态提供的。
我可以以某种方式告诉 Spring(通过存储库上的注释或通过配置)使用哪个存储库?或者我是否必须像这样在 Spring Data JPA 存储库中使用 @Primary 进行手动解决,还是应该提出一种存储库提供程序而不是自动装配?
编辑以使事情更清楚:假设我有一个实体A
@Entity
public class A {
@Id
private long id;
}
Run Code Online (Sandbox Code Playgroud)
及其存储库
public ARepository extends Repository<A, Long> {
}
Run Code Online (Sandbox Code Playgroud)
现在我将它扩展到实体 B
@Entity
public class B extends A {
}
public interface BRepository extends ARepository {
}
Run Code Online (Sandbox Code Playgroud)
通常,根据文档,您使用这样的存储库:
@Autowired
private ARepository repository;
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为现在有两个类型为 的 bean ARepository。对于我自己实现的 bean,我会@Primary在扩展类上使用,但对于存储库,在编译时没有接口的实现。
我将 Spring MVC 与 Spring Boot 和 Thymeleaf 结合使用。我有返回 Thymeleaf 模板名称的普通控制器和用@RepsonseBody.
假设我有一个EntityNotFoundException由控制器调用的某些代码引发的错误。如果抛出它,我想分别返回 404 状态代码和 REST 控制器的错误页面或错误消息。
我当前对普通控制器的设置:
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
@Controller
public class FooController {
@RequestMapping("/foo") public String foo() {
try {
...
} catch (EntityNotFoundException enfe) {
throw new ResourceNotFoundException(enfe.getMessage());
}
return "foo";
}
}
Run Code Online (Sandbox Code Playgroud)
对于 REST 控制器,我不会捕获异常并让全局异常处理程序拾取它:
@Controller
public class BarController {
@RequestMapping("/bar")
@ResponseBody
public SomeDto bar() throws EntityNotFoundException {
...
return someDto; …Run Code Online (Sandbox Code Playgroud) 我可以使类通过Groovy的编译时元编程实现接口吗?如果可以,如何实现?我知道我可以实现给定类的接口中定义的方法。但是,如何将此类的对象转换为该接口类型呢?
假设我有一个界面
public interface MyInterface {
void foo();
}
Run Code Online (Sandbox Code Playgroud)
和一堂课
public class MyClass {
}
Run Code Online (Sandbox Code Playgroud)
可以予然后提供一种方法,bar与返回类型MyInterface返回的实例MyClass调用时等
MyInterface mi = bar();
mi.foo();
Run Code Online (Sandbox Code Playgroud)
并且不提出ClassCastException?
java ×3
spring ×2
groovy ×1
hibernate ×1
jpa ×1
linux ×1
redmine ×1
spring-boot ×1
spring-data ×1
spring-mvc ×1
thin ×1