【问题标题】:Not able to get auto generated Id, using JPA无法使用 JPA 获取自动生成的 ID
【发布时间】:2020-05-31 06:13:40
【问题描述】:

我有两个实体。一个是 UserEntity,另一个是 TaskEntity。

@Entity
@Table(name="user")
public class UserEntity {
    @Id
    private String userEmail;
    private String password;
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="user_email")
    private List<TaskEntity> tasks;

    //getter setter for variables
}
@Entity
@Table(name="task")
public class TaskEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private String description;
    private String statusDate;
    private String status;

    //getter setter for variables
}

现在我想创建一个基于 userEmail 的新任务,所以我在 DAO 类中这样做:

    @PersistenceContext
    EntityManager em;
    public Integer addNewTaskByUserEmail(Task task, String userEmail) {
        UserEntity userEntity = em.find(UserEntity.class, userEmail);
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setName(task.getName());
        taskEntity.setDescription(task.getDescription());
        taskEntity.setStatus(task.getStatus());
        taskEntity.setStatusDate(task.getDate());
        userEntity.getTasks().add(taskEntity);
        return taskEntity.getId();
    }

但是在我的服务类中我得到 null 的返回语句中。如何返回自动生成的 taskId?

【问题讨论】:

    标签: java hibernate spring-boot spring-data-jpa one-to-many


    【解决方案1】:

    一个可能的问题是您没有保存与用户关联的任务。保存任务,然后您可能能够获取 taskId。

    public Integer addNewTaskByUserEmail(Task task, String userEmail) {
        UserEntity userEntity = em.find(UserEntity.class, userEmail);
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setName(task.getName());
        taskEntity.setDescription(task.getDescription());
        taskEntity.setStatus(task.getStatus());
        taskEntity.setStatusDate(task.getDate());
        em.getTransaction().begin();
        em.persist(taskEntity);
        em.getTransaction().commit();
        userEntity.getTasks().add(taskEntity);
        return taskEntity.getId();
    }
    

        @Autowired TaskRepository taskRepository
    
        public Integer addNewTaskByUserEmail(Task task, String userEmail) {
            UserEntity userEntity = em.find(UserEntity.class, userEmail);
            TaskEntity taskEntity = new TaskEntity();
            taskEntity.setName(task.getName());
            taskEntity.setDescription(task.getDescription());
            taskEntity.setStatus(task.getStatus());
            taskEntity.setStatusDate(task.getDate());
            taskEntity = taskRepository.save(taskEntity)
            userEntity.getTasks().add(taskEntity);
            return taskEntity.getId();
        }
    

    TaskRepository 将在哪里

    @Repository
    public interface TaskRepository extends JpaRepository<TaskEntity, Integer>  
    {  
    
    }  
    

    【讨论】:

    • 我在您建议的第一种方法中遇到错误,我没有得到这个 TaskRepository 是什么。你能解释一下吗,我是 JPA 的新手。
    • 我添加了TaskRepository,它提供了执行某些操作的基本方法,例如save、findById、findAll等。
    • 对于第一种方法,请用详细的堆栈跟踪更新问题
    • 你好@Ashsih,你添加了这些吗?
    • 不,我不知道 JpaRepository。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2018-06-22
    • 1970-01-01
    相关资源
    最近更新 更多