2017-04-25 26 views
0

我有两个实体,用户和操作和两个实体拥有其中一个连接:加入实体之前保存使用POST方法

@Entity 
public class User implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long userId; 

    @Basic 
    private String username; 

    private String password; 

    //Getters and Setters 
} 

@Entity 
public class Operation implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long userId; 

    @ManyToOne 
    @JoinColumn(name = "user_id") 
    User user; 

    //Getters and Setters 

} 

两个实体拥有仓库了。

在我的上下文中,当用户(操作员)已被记录时,用户实体被加载到会话范围(HttpSession)中。 对于系统上用户的每个操作,应用程序通过Operation Repository注册该操作。

我的问题是:如何在数据库中注册之前设置用户实体(进行会话)操作?

是否有可能重写存储库方法?

编辑1:

该操作通过使用HTTP POST方法的Web界面保存。我需要继续使用URI来保存。像:

URI:HTTP://本地主机:9874 /操作 数据:{ “名”: “操作名称”}

谢谢!

+0

你的意思重写由Spring数据JPA提供的保存方法是什么? – pvpkiran

+0

@pvpkiran到底!我认为这是我的解决方案... –

回答

3

您可以创建一个预先保存的事件处理程序,可以在其中设置的关联:你然后可以将标准的Spring Data Rest帖子设置为http://localhost:9874/operations,并且不需要定制存储库或控制器。

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#_writing_an_annotated_handler

@RepositoryEventHandler 
public class OperationEventHandler { 

    @HandleBeforeSave 
    public void handleOperationSave(Operation operation) { 

    } 
} 

你说用户存储在会话。我认为它不是在使用Spring Security?如果你是,那么你可以使用一个静态调用获取当前用户:

SecurityContextHolder.getContext().getAuthentication(); 

否则你将需要尝试和电线HttpServletRequest的,以你的事件处理程序或使用静态包装呼叫作为这些问题的答案概述:

Spring: how do I inject an HttpServletRequest into a request-scoped bean?

从这里就可以得到HttpSession中。

在HttpServletRequest的下面显示布线正是这种情况下

Spring Data Rest - How to receive Headers in @RepositoryEventHandler

所以你的处理器看起来类似:

@RepositoryEventHandler 
public class OperationEventHandler { 

    @Autowired 
    private HttPServletRequest request; 

    @HandleBeforeSave 
    public void handleOperationSave(Operation operation) { 

     User user = (User)request.getSession().getAttribute("userKey"); 
     operation.setUser(user); 
    } 
} 
+0

感谢Alan给出了很好的答案!是的,我正在使用SB安全。对不起,不说...但是,它对我来说非常好,但是我添加了一件事......需要在OperationEventHandler中添加注解@Component ...其他的事情,如果我调用.save()方法从仓库手动这个处理程序也会被调用? –

+1

不是。它是特定于SDR的。如果你不需要注入任何东西,你可以使用标准的JPA事件监听器,它应该可以在两种情况下工作。 http://www.objectdb.com/java/jpa/persistence/event –

0

创建一个自定义存储库接口并为此写一个实现。例如。

public interface OperationRepositoryCustom { 

    <T extends Operation> T saveCustomized(Operation operation); 
} 

你的实现类看起来像这样。

public class OperationRepositoryImpl implements OperationRepositoryCustom{ 
    //You can autowire OperationRepository and other dependencies 

    @Autowired 
    OperationRepository operationRepository; 

    @Override 
    public Operation saveCustomized(Operation operation) { 
     //put your code to update operation with user and save it by calling operationRepository.save(). 
    } 
} 

请注意命名约定,即您的自定义实现需要具有与您的存储库+ Impl相同的名称。所以,如果你的资料​​库界面被称为OperationRepository,您的自定义库界面应该OperationRepositoryCustom并实现了一套应该被命名OperationRepositoryImpl

希望它可以帮助

+0

感谢您快速回答!我忘了谈一件事!我使用REST API来保存操作。如果我使用你的答案,我会打破我的应用程序,对吧? –