【问题标题】:Hibernate JPA Update dataHibernate JPA 更新数据
【发布时间】:2019-05-27 05:06:58
【问题描述】:

我有一个包含 3 个实体的数据库:用户、配置文件和用户配置文件。当我更新用户时,我有一个错误。

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_ where 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",
            }
        }
    ]
}

【问题讨论】:

  • 它表示您的数据库架构中没有名为 hibernate_sequence 的表。

标签: spring hibernate jpa spring-data-jpa


【解决方案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);
    }
}

【讨论】:

  • 我使用 postgres SQL。序列是 seq_user、seq_profile、seq_user_profile
  • 我用正确的序列名称更新了我的答案。你试过吗?
  • 我有另一个错误:org.hibernate.SQL:插入用户配置文件(id_profile,id_user,id)值(?,?,?)ohengine.jdbc.spi.SqlExceptionHelper:SQL错误: 0, SQLState: 23502 ohengine.jdbc.spi.SqlExceptionHelper : 错误: NULL 值违反了“id_user”列的 NOT NULL 约束详细信息: 失败的行包含 (951, null, 1) 我不明白为什么因为我做的是更新而不是插入。对于插入是否在我没有修改这部分用户时生成我已经从顶部更新了我的第一篇文章
  • 您必须将反向引用表单 UserProfile 设置为 User 因为这不包含在 JSON 中。 UserProfile 中的引用用户负责存储外键
  • 请问有什么解决办法
猜你喜欢
  • 1970-01-01
  • 2019-09-09
  • 2013-04-30
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 2020-05-23
  • 2019-08-12
  • 1970-01-01
相关资源
最近更新 更多