小编Ros*_*ion的帖子

Spring Data JPA插入而不是Update

嗨,我是Sprig Data JPA的新手,我很想知道即使我将Id传递给实体,Spring数据jpa也是插入而不是合并.我想当我实现Persistable接口并实现这两个方法时:

public Long getId();
public Boolean isNew();
Run Code Online (Sandbox Code Playgroud)

它会自动合并而不是持久化.

我有一个名为User的实体类:

@Entity
@Table(name = "T_USER")
public class User implements Serializable, Persistable<Long> {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "USER_ID")
   private Long id;

   @Column(name = "CREATION_TIME", nullable = false)
   private Date creationTime;

   @Column(name = "FIRST_NAME", nullable = false)
   private String firstName;

   @Column(name = "LAST_NAME", nullable = false)
   private String lastName;

   @Column(name = "MODIFICATION_TIME", nullable = false)
   private Date modificationTime;
Run Code Online (Sandbox Code Playgroud)

还有另一堂课

@Entity
@Table(name = "T_USER_ROLE")
public class UserRole …
Run Code Online (Sandbox Code Playgroud)

spring jpa spring-data-jpa

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

为什么@Scheduled注释不适用于@Transaction批注.春季启动

我有一个问题:为什么当我们使用@Scheduledand 注释方法时@Transaction,事务不起作用?我知道@Scheduled调用我的类而不是Spring创建的代理类,但无法理解这种行为.

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Override
    @Scheduled(fixedRateString = "${somestring}",initialDelayString = "${anotherstring}")
    @Transactional
    public void doSomething() {

        }
    }
Run Code Online (Sandbox Code Playgroud)

我有两个解决这个问题的方法:

  1. Scheduled方法调用代理.

  2. 用代理ConcurrentTaskScheduler 对象实现和替换ScheduledMethodRunnable(与我的类一起)的对象ScheduledMethodRunnable.

但是这种解决方案非常不方便.

你能解释一下为什么会@Scheduled这样吗?

谢谢!

java spring scheduled-tasks spring-transactions spring-boot

9
推荐指数
1
解决办法
3143
查看次数

使用 Blob 下载 JavaScript 文件

我正在尝试从 JS 下载该文件。服务器在 application/octect-stream 中发送流。在 JS 中我正在这样做:

var blob = new Blob([this.response], {type: "application/octet-stream"});
var downloadUrl = window.URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(downloadUrl);
Run Code Online (Sandbox Code Playgroud)

但是当打开下载的文件时,它只是空的。文件可以是 jpeg、pdf、txt 和其他 mime 类型。

javascript

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

JPA 自引用实体

我有一个名为 User 的表,如下所示:

public class User
{
    private Long userId;
    private String username;
    //Other fields;
}
Run Code Online (Sandbox Code Playgroud)

现在假设用户可以有其他用户朋友。那么我如何在 JPA 中创建这种关系。

与此同时,我在数据库的用户表中执行此操作:

USER_ID 
USERNAME
USER_FRIENDS
Run Code Online (Sandbox Code Playgroud)

在用户实体中像这样:

public class User
{
    private Long userId;
    private String username;
    //Other fields;
    @OneToMany(cascade = CascadeType.ALL)
    @Column(name = "USER_FRIENDS")
    private List<UserEntity> friends;
}
Run Code Online (Sandbox Code Playgroud)

这是行不通的。那么,我如何在 JPA 中实现这一目标呢?提前致谢!

java hibernate jpa

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