Java 9 引入了工厂方法来使用方法创建不可变列表List.of()。
哪个更适合创建一个元素的不可变列表?
List<String> immutableList1 = List.of("one");List<String> immutableList2 = Collections.singletonList("one");我有 2 个表如下:
TABLE_A
-------
A_SIREN
A_NDA
TABLE_B
-------
B_id
B_NDA
B_SIREN
Run Code Online (Sandbox Code Playgroud)
表 A 的 id 是COMPOSITE KEYSIREN/NDA
这是实体代码。
重点班
@Embeddable
public class SirenNdaKey implements Serializable {
@Column(name = "A_SIREN")
protected String siren;
@Column(name = "A_NDA")
protected String nda;
// getters setters
}
Run Code Online (Sandbox Code Playgroud)
表A
public class EntityA{
@EmbeddedId
private SirenNdaKey sirenNda;
@OneToMany(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumns({ @PrimaryKeyJoinColumn(name = "A_SIREN",
referencedColumnName = "B_SIREN"),
@PrimaryKeyJoinColumn(name = "A_NDA", referencedColumnName = "B_NDA")
})
private Set<EntityB> EntityBSet;
...
}
Run Code Online (Sandbox Code Playgroud)
表B
public class EntityB
@Id …Run Code Online (Sandbox Code Playgroud) 我正在使用 PropertyPlaceholderConfigurer 来获取 Spring 计划注释配置中的 java 属性
@Scheduled(cron = "${execution.frequency}")
public void generateReport() {
Run Code Online (Sandbox Code Playgroud)
当没有定义覆盖属性值时,有没有办法指定默认值?
Spring 3支持 ${my.property:defaultValue} 语法,但在注释上不允许,我尝试传递变量但也不允许,注释属性 Scheduled.cron 的值必须是常量表达式
为什么这个代码
"name".equals(person.getName());
Run Code Online (Sandbox Code Playgroud)
比...更好
person.getName().equals("name");
Run Code Online (Sandbox Code Playgroud) 怎么了,我试着在评论列表中添加新评论,评论被映射到Post类的列表.
这是我的代码.
Post.java
@Document
public class Post {
@Id
private String id;
@DBRef
private List<Comment> comments;
public void addComment(Comment comment) {
if (comments == null) {
comments = new ArrayList<>();
}
this.comments.add(comment);
}
// getters and setters....
}
Run Code Online (Sandbox Code Playgroud)
Comment.java
@Document
public class Comment {
@Id
private String id;
private String comment;
private int rating;
// getters and setters....
}
Run Code Online (Sandbox Code Playgroud)
的Test.class
@Test
public void savePostWithComments() {
Post post = postRepository.findAll().get(1);
Comment comment = new Comment();
comment.setComment("comment");
comment.setRating(5);
post.addComment(comment);
postRepository.save(post);
}
Run Code Online (Sandbox Code Playgroud)
测试因此错误而失败 …
java ×5
spring ×2
hibernate ×1
java-9 ×1
jpa ×1
mongodb ×1
one-to-many ×1
properties ×1
spring-data ×1