Baa*_*aDe 4 java hibernate jpa h2 spring-boot
我正在尝试关联两个表
\n用户和地址\n一个用户有一个地址,一个地址只属于一个用户。键按地址 ID 列出\n所以我首先创建我的地址,然后创建一个用户并将其与地址 ID 链接\n但我根本无法做到这一点,我收到以下错误:
\nError creating bean with name \'entityManagerFactory\' defined in class path resource [org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot invoke "org.hibernate.mapping.PersistentClass.getTable ()" because "classMapping" is null\nRun Code Online (Sandbox Code Playgroud)\n我对休眠完全陌生,但我需要这个大学项目,所以请原谅我对这个主题的无知
\n那是我的代码:
\n用户/USUARIO 类别:
\nimport org.hibernate.validator.constraints.br.CPF;\n\nimport javax.persistence.*;\nimport javax.validation.constraints.*;\n\n\npublic class Usuario{\n\n @Id\n @GeneratedValue(strategy = GenerationType.IDENTITY)\n private Integer id;\n\n @Column\n @NotNull\n @Size(min = 5,max = 30)\n @Pattern(regexp = "^[a-zA-Z\\s]*$", message = "Nome inv\xc3\xa1lido! Digite apenas letras e espa\xc3\xa7amento") //Permite apenas letras e espa\xc3\xa7o\n private String nome;\n\n @NotNull\n @CPF\n private String cpf;\n\n @NotNull\n @Email\n private String email;\n\n @NotNull\n @Size(min = 5,max = 12)\n private String senha;\n\n private Integer telefone;\n\n @DecimalMin("0")\n @DecimalMax("5")\n private Double avaliacao;\n\n @NotNull\n @OneToOne(cascade = CascadeType.ALL,mappedBy = "id")\n private Endereco endereco;\n\n //Atributos para usu\xc3\xa1rios aut\xc3\xb4nomos\n\n private Boolean isAutonomo;\n\n private String categoriaAutonomo;\n\n private Double precoAutonomo;\n\n//Getters and Setters\nRun Code Online (Sandbox Code Playgroud)\n地址/ENDERECO 类
\nimport javax.persistence.*;\nimport javax.validation.constraints.NotNull;\nimport javax.validation.constraints.Size;\n\n@Entity\n@Table(name = "endereco")\npublic class Endereco {\n\n @Id\n @OneToOne\n @GeneratedValue(strategy = GenerationType.AUTO)\n private Integer id;\n\n @NotNull\n @Size(min = 8,max = 8)\n private String cep;\n\n @NotNull\n private String bairro;\n\n @NotNull\n private String logradouro;\n\n @NotNull\n private Integer numeroLogradouro;\n\n private String complemento;\n\n @NotNull\n @Size(min = 2,max = 2)\n private String uf;\n\n @NotNull\n private String cidade;\nRun Code Online (Sandbox Code Playgroud)\n控制器
\nimport br.com.bandtec.projetocaputeam.dominio.*;\nimport br.com.bandtec.projetocaputeam.repository.*;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.http.ResponseEntity;\nimport org.springframework.web.bind.annotation.*;\n\nimport javax.validation.Valid;\nimport java.util.List;\n\n@RestController\n@RequestMapping("/caputeam")\npublic class CaputeamController {\n\n @Autowired\n private UsuariosRepository usuariosRepository;\n\n @Autowired\n private EnderecoRepository enderecoRepository;\n\n//--- USERS\n @GetMapping("/usuarios")\n public ResponseEntity getUsuarios(){\n List<Usuario> usuarios = usuariosRepository.findAll();\n return !usuarios.isEmpty() ? ResponseEntity.status(200).body(usuarios) :\n ResponseEntity.status(204).build();\n }\n\n @PostMapping("/cadastrar-usuario")\n public ResponseEntity cadastrarUsuario(@RequestBody @Valid Usuario novoUsuario){\n usuariosRepository.save(novoUsuario);\n return ResponseEntity.ok().build();\n }\n\n//--- ADRESS\n @PostMapping("/cadastrar-endereco")\n public ResponseEntity cadastrarEndereco(@RequestBody @Valid Endereco novoEndereco){\n enderecoRepository.save(novoEndereco);\n return ResponseEntity.ok().build();\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n应用
\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n\n@SpringBootApplication\npublic class ProjetoCaputeamApplication {\n\n public static void main(String[] args) {\n SpringApplication.run(ProjetoCaputeamApplication.class, args);\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n编辑\n我尝试删除“映射者”部分并从地址中删除@OneToOne,但现在当我尝试发送地址的 POST 时,它会返回以下错误:
\norg.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Referential integrity constraint violation: "FKMXNOON0IKGA83W1A203Y6OFPN: PUBLIC.ENDERECO FOREIGN KEY(ID) REFERENCES PUBLIC.USUARIO(ID) (1)"; SQL statement:\ninsert into endereco (bairro, cep, cidade, complemento, logradouro, numero_logradouro, uf, id) values (?, ?, ?, ?, ?, ?, ?, ?) [23506-200]\nRun Code Online (Sandbox Code Playgroud)\n就好像他没有\xe2\x80\x99t 输入任何地址字段一样
\n我通过邮递员发送我的帖子,如下所示:
\n{\n "bairro": "Vila Prmavera",\n "cep": "03388110",\n "cidade": "S\xc3\xa3o Paulo",\n "complemento": "b1",\n "logradouro": "Rua das dores",\n "numeroLogradouro": 7,\n "uf": "SP"\n}\nRun Code Online (Sandbox Code Playgroud)\n
不要映射到 ID。Map 表示实体映射而不是 ID 映射。
public class Endereco {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne
private Usuario usuario
....
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您不希望 Endereco 保留对 Usuario 的引用,只需将其删除即可。但你不能放在@OneToOneid 字段上。如果您只有一侧,那么@OneToOne您还需要注释 @MapsId。
public class Usario {
@NotNull
@MapsId
@OneToOne(cascade = CascadeType.ALL)
private Endereco endereco;
public class Endereco {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id
}
Run Code Online (Sandbox Code Playgroud)
因为@OneToOne尝试与实体映射,这意味着映射到数据库中的表。对于 id,数据库中没有任何实体或表。这就是为什么它抱怨
| 归档时间: |
|
| 查看次数: |
6793 次 |
| 最近记录: |