Hibernate JPA更新数据

时间:2018-12-30 00:18:10

标签: spring hibernate jpa spring-data-jpa

我有一个包含3个实体的数据库:User,Profile和UserProfile。更新用户时出现错误。

public class User implements Serializable {
  private static final long serialVersionUID = 1L;

  @Id
  @SequenceGenerator(name = "seqUser", sequenceName = "seqUser")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqUser")
  private Integer id;

  private String email;

  @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  private List<UserProfile> userProfiles = new ArrayList<UserProfile>();

}

还有

 public class Profile implements Serializable {
      private static final long serialVersionUID = 1L;

      @Id
      @SequenceGenerator(name="seqProfile", sequenceName ="seqProfile")
      @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqProfile")
      private Integer id;

      private String codeProfile;

      private String libelleProfile;

      @OneToMany(mappedBy="profile", cascade = CascadeType.ALL,  fetch = FetchType.LAZY)
      @JsonIgnore
      private List<UserProfile> userProfiles = new ArrayList<UserProfile>();
    }

 public class UserProfile implements Serializable {
      private static final long serialVersionUID = 1L;

      @Id
      @SequenceGenerator(name = "seqUserProfile", sequenceName = "seqUserProfile")
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqUserProfile")
      private Integer id;

      @ManyToOne
      @JoinColumn(name = "idUser")
      @JsonIgnore
      private User user;

      @ManyToOne
      @JoinColumn(name = "idProfile")
      private Profile profile;
    }

我的存储库:

public interface IUserRepository extends JpaRepository<User, Integer>{
  User findByEmail(String email);
}

我的服务:

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private IUserRepository userRepository;

    public User getUserByEmail(String email) {

        return userRepository.findByEmail(email);
    }

    public void updateUser(User user) {
        userRepository.save(user);
    }

}

我的控制器:

@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @PutMapping(value="/user/{id}", consumes=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> updateUser(
            @PathVariable(value="id", required = true) Integer id,
            @RequestBody User user) {           

        userService.updateUser(user);

        return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
    }
}

日志和错误:

  

休眠:从用户user0_中选择user0_.id作为id1_19_1_,将user0_.email作为email4_19_1_,其中user0_.id =?
  休眠:从profil profil0_中选择profil0_.id作为id1_13_0_,profil0_.code_profil作为code_pro2_13_0_,profil0_.libelle_profil作为libelle_3_13_0_,其中profil0_.id =?
  2018-12-30 00:38:15.385调试10692 --- [nio-8080-exec-1] org.hibernate.SQL:选择nextval('hibernate_sequence')休眠:选择nextval('hibernate_sequence')
  2018-12-30 00:38:15.389 WARN 10692 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:0,SQLState:42P01
  2018-12-30 00:38:15.389错误10692-[[nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper:错误:关系«hibernate_sequence»不存在位置:17

我的json流量:

{
    "id": 1,
    "email": "email",
    "userProfiles": [
        {
            "profile": {
                "id": 1,
                "codeProfile": "ADMIN",
                "libelleProfile": "Administrateur",
            }
        },
        {
            "profile": {
                "id": 2,
                "codeProfile": "PROFILE2",
                "libelleProfile": "Profile 2",
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:-1)

您正在使用哪个数据库?这个数据库是否支持序列?

但除此之外,@ SequenceGenerator中缺少sequenceName属性:

@Id
@SequenceGenerator(name = "seq_user", sequenceName = "seq_user")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_user")
private Integer id;

此sequenceName必须是数据库中序列的名称。

接下来,您必须确保在UserProfile类中设置对User的反向引用

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private IUserRepository userRepository;

    public User getUserByEmail(String email) {

        return userRepository.findByEmail(email);
    }

    public void updateUser(User user) {
        for (UserProfile profile : user.getUserProfiles()) {
          profile.setUser(user);
        }
        userRepository.save(user);
    }
}