当我执行插入操作时,traduction中的外键(idMot引用mot)为null,这是因为当我前台发送正确的外键时。我最终遇到此错误: not-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 */;
这是我的web服务:(在此过程中,idMot显示为空)
@POST
@Path("/create")
public Traduction createTraduction(Traduction traduction) throws Exception {
return FacadeBackOffice.getInstance().getTraductionService().createTraduction(traduction);
}
有人可以解决吗?
答案 0 :(得分:0)
在多对一映射中也全部使用级联类型。
@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;