-- parent table
CREATE TABLE subjects
(
subject_id Bigint NOT NULL AUTO_INCREMENT,
name Varchar(250) NOT NULL,
weight Int,
color Varchar(20),
ontology_id Bigint NOT NULL,
created_by Bigint NOT NULL,
created_at Datetime NOT NULL,
updated_by Bigint,
updated_at Datetime,
PRIMARY KEY (subject_id)
);
-- child table
CREATE TABLE phrases
(
phrase_id Bigint NOT NULL AUTO_INCREMENT,
name Varchar(250) NOT NULL,
weight Int,
color Varchar(20),
subject_id Bigint NOT NULL,
created_by Bigint NOT NULL,
created_at Datetime NOT NULL,
updated_by Bigint,
updated_at Datetime,
PRIMARY KEY (phrase_id)
);
ALTER TABLE phrases ADD CONSTRAINT phrases_subjects_fk FOREIGN KEY (subject_id) REFERENCES subjects (subject_id);
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name = "subjects")
public class Subject implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "subject_id")
private Long subjectId;
@NotEmpty
@Size(max = 250)
@Column(name = "name", length = 250, nullable = false)
private String name;
@NotNull
@Column(name = "weight")
private Integer weight;
@Size(max = 20)
@Column(name = "color", length = 20)
private String color;
@NotNull
@Column(name = "ontology_id")
private Long ontologyId;
@OneToMany(mappedBy = "subject", targetEntity = Phrase.class, fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
private Set<Phrase> phrases;
@Column(name = "created_by")
private Long createdBy;
@JsonIgnore
@CreationTimestamp
@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
@Column(name = "updated_by")
private Long updatedBy;
@JsonIgnore
@UpdateTimestamp
@Column(name = "updated_at")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;
public Subject() {
// empty constructor
}
public Subject(@JsonProperty(value = "subjectId") Long subjectId,
@JsonProperty(value = "name") String name,
@JsonProperty(value = "weight") Integer weight,
@JsonProperty(value = "color") String color,
@JsonProperty(value = "ontologyId") Long ontologyId,
@JsonProperty(value = "phrases") Set<Phrase> phrases,
@JsonProperty(value = "createdBy") Long createdBy,
@JsonProperty(value = "updatedBy") Long updatedBy) {
super();
this.subjectId = subjectId;
this.name = name;
this.weight = weight;
this.color = color;
this.ontologyId = ontologyId;
this.phrases = phrases;
this.createdBy = createdBy;
this.updatedBy = updatedBy;
}
public Long getSubjectId() {
return subjectId;
}
public void setSubjectId(Long subjectId) {
this.subjectId = subjectId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Long getOntologyId() {
return ontologyId;
}
public void setOntologyId(Long ontologyId) {
this.ontologyId = ontologyId;
}
public Set<Phrase> getPhrases() {
return phrases;
}
public void setPhrases(Set<Phrase> phrases) {
this.phrases = phrases;
}
public Long getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Long createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Long getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(Long updatedBy) {
this.updatedBy = updatedBy;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
}
package com.gda.wControl.domain;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.validator.constraints.NotEmpty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name = "phrases")
public class Phrase implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5778951738523949512L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "phrase_id")
private Long phraseId;
@NotEmpty
@Size(max = 250)
@Column(name = "name", length = 250, nullable = false)
private String name;
@NotNull
@Column(name = "weight")
private Integer weight;
@Size(max = 20)
@Column(name = "color", length = 20)
private String color;
@ManyToOne
@JoinColumn(name = "subject_id", referencedColumnName = "subject_id")
private Subject subject;
@Column(name = "created_by")
private Long createdBy;
@JsonIgnore
@CreationTimestamp
@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
@Column(name = "updated_by")
private Long updatedBy;
@JsonIgnore
@UpdateTimestamp
@Column(name = "updated_at")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;
public Phrase() {
// empty constructor
}
public Phrase(@JsonProperty(value = "phraseId") Long phraseId,
@JsonProperty(value = "name") String name,
@JsonProperty(value = "weight") Integer weight,
@JsonProperty(value = "color") String color,
@JsonProperty(value = "subject") Subject subject,
@JsonProperty(value = "createdBy") Long createdBy,
@JsonProperty(value = "updatedBy") Long updatedBy) {
super();
this.phraseId = phraseId;
this.name = name;
this.weight = weight;
this.color = color;
this.subject = subject;
this.createdBy = createdBy;
this.updatedBy = updatedBy;
}
public Long getPhraseId() {
return phraseId;
}
public void setPhraseId(Long phraseId) {
this.phraseId = phraseId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
public Long getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Long createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Long getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(Long updatedBy) {
this.updatedBy = updatedBy;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
}
@ApiOperation(value = "add a new subject")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public Subject addSubject(@Valid @RequestBody Subject Subject) {
return subjectService.createSubject(Subject);
}
public Subject createSubject(Subject subject) {
Subject existingSubject = subjectRepository.findByNameAndOntologyId(
subject.getName().toLowerCase(), subject.getOntologyId());
if (existingSubject != null) {
throw new ValidationException(String.format(SUBJECT_ALREADY_EXIST,
subject.getName(), subject.getOntologyId()));
}
return subjectRepository.saveAndFlush(subject);
}
{
"name": "string444",
"weight": 0,
"color": "string",
"ontologyId": 1,
"phrases": [
{
"name": "string",
"weight": 0,
"color": "string",
"createdBy": 1
}
],
"createdBy": 1
}
似乎没有在subject(parent)对象中设置短语(child)对象。听起来像是API调用。传递此信息并试图添加主题的人不是在设置客户端对象。
我还注意到setPhrases settter在Subject实体中不可用。
请确保您有设置子对象的setter方法,并确保传递subject对象的人已经设置了子对象。
一天中的好时光!我在使用Hibernate创建多对多关系时遇到一些问题。它在联接表中创建唯一约束: “uk_bapa98k9j6y66sqniad6k680l”唯一约束,btree(用户id) 因此,我只能在此表中为特定用户设置一行,尝试插入具有相同user_id的另一行会导致错误: 错误组织。冬眠发动机jdbc。spi。SqlExceptionHelper-错误:重复的键值违反了唯一约束“uk_
我有两个表模式 员工 地址
我有两个表,ComputerNode和Connection。ComputerNode有一个主键< code>nodeid,但连接没有主键。我无法修改表模式。如果它们有一对多的关系,我应该如何创建java POJO?基本上我的目标是做一个像这样的内部连接: 下面是 SQL 表架构。计算机节点表: 连接表: 表之间的关系是一对多的。一个计算机节点可以有多个连接 我已经创建了两个Java的POJO类,但
我在hibernate中搜索建立关系的各种可能性,遇到了下面的代码片段
我的应用程序在域级值对象之间使用了大量OneToMore和OneToOne引用,其中大多数是实体,要么是超类,要么是某物的子类。我想为我的应用程序提供一种一致(但简单)的方法来保存这些实例,实际的方法保存()就是这样 当前的问题是如何正确地实例化这些对象,以及在cassadeType中选择哪种策略,我想在保存带有其他实体引用的对象时保存嵌套对象,它现在可以工作,但只是第一次,之后我得到了一个,因为
我正在学习冬眠,只是有点困惑。