[英]Spring Hibernate JSP - Foreign key is null when trying to save a record
[英]Hibernate - Null foreign key when trying to do an insert
當我執行插入操作時,traduction中的外鍵(idMot引用mot)為null,因為我的前台發送了正確的外鍵。 我最終遇到此錯誤: not-null屬性引用了null或瞬態值:com.virtual.expertise.mydico.model.Traduction.mot
我想我在模型中做錯了什么,但仍然不知道是什么。
這是交易模型:
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "langue")
private String langue;
@Column(name = "traduction")
private String traduction;
@JsonBackReference(value = "mot-traduction")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "idMot", nullable = false, insertable = true,
updatable = false)
private Mot mot;
@CreationTimestamp
@Column(name = "dateCreation")
private Date dateCreation;
這是mot模型:
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "libelle")
private String libelle;
@JsonBackReference(value = "user-mot")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "idUser", nullable = false, insertable = true, updatable = false)
private User user;
/** les traductions. */
@JsonManagedReference(value = "mot-traduction")
@OneToMany(mappedBy = "mot", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Traduction> traductions;
@CreationTimestamp
@Column(name = "dateCreation")
private Date dateCreation;
這是轉導表:
DROP TABLE IF EXISTS `traduction`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `traduction` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`langue` varchar(100) DEFAULT NULL,
`traduction` varchar(100) DEFAULT NULL,
`idMot` bigint(20) DEFAULT NULL,
`dateCreation` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idMot_idx` (`idMot`),
CONSTRAINT `idMot` FOREIGN KEY (`idMot`) REFERENCES `mot` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
這是我的webService :(在其中idMot出現為null)
@POST
@Path("/create")
public Traduction createTraduction(Traduction traduction) throws Exception {
return FacadeBackOffice.getInstance().getTraductionService().createTraduction(traduction);
}
有人可以解決嗎?
也可以在多對一映射中全部使用級聯類型。
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "langue")
private String langue;
@Column(name = "traduction")
private String traduction;
@JsonBackReference(value = "mot-traduction")
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumn(name = "idMot", nullable = false, insertable = true,
updatable = false)
private Mot mot;
@CreationTimestamp
@Column(name = "dateCreation")
private Date dateCreation;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.