I have a Question
Entity and Tag
entity with getter, setter methods and a OneToMany
relationship from question
to tag
and a OneToOne
relationship from question
to user
public class Question {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="title")
private String title;
@Column(name="body")
private String body;
@Temporal(TemporalType.DATE)
@Column(name="date_created")
private Date date_created;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="user_id")
private User user;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="tag_id")
private Tag tag;
@Column(name="answer_count")
private int answer_count;
@Column(name="view_count")
private int view_count;
public Question() {
}
Tag entity
public class Tag {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="name")
private String name;
@Column(name="username")
private String username;
@Temporal(TemporalType.DATE)
@Column(name="date_created")
private Date date_created;
public Tag() {
}
When I try to insert a question using Postman with the following details:
{
"title": "stefanyyyxx",
"body": "stefandoyee44",
"date_created": "2019-02-27",
"user_id" : 1,
"tag_id": 1,
"answer_count": 0,
"view_count": 0
}
QuestionRepository.java:
@Override
public void save(Question theQuestion) {
// get the current hibernate session
Session currentSession = entityManager.unwrap(Session.class);
// save employee
currentSession.saveOrUpdate(theQuestion);
}
Null values are being inserted for user_id
and tag_id
though I used JoinColumn()
.
MySQL:
As @Karol Dowbecki Suggested,
convert the JSON to DTO object and use that DTO to get the User
, Tag
Entities from their respective repositories .
Finally create the Question entity object and store it.
Question Entity
@Entity
@Table(name = "question")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title")
private String title;
@Column(name = "body")
private String body;
@Temporal(TemporalType.DATE)
@Column(name = "date_created")
private Date dateCreated;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private User user;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "tag_id")
private Set<Tag> tag;
@Column(name = "answer_count")
private int answerCount;
@Column(name = "view_count")
private int viewCount;
}
User Entity
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
Tag Entity
@Entity
@Table(name = "tag")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "username")
private String username;
@Temporal(TemporalType.DATE)
@Column(name = "date_created")
private Date dateCreated;
}
DTO Class
public class QuestionDTO {
private Long id;
private String title;
private String body;
private Date dateCreated;
private Long user;
private Long tag;
private int answerCount;
private int viewCount;
}
Test Class
@Service
public class TestService {
@Autowired
private QuestionRepository questionRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private TagRepository tagRepository;
public void addQuestion(QuestionDTO dto) {
Tag tag = null;
User user = null;
Question question = null;
Set<Tag> tags = null;
tag = tagRepository.findById(dto.getTag());
tags = new HashSet<>();
tags.add(tag);
user = userRepository.findById(dto.getUser());
question = new Question();
question.setTag(tags);
question.setUser(user);
question.setId(dto.getId());
question.setBody(dto.getBody());
question.setTitle(dto.getTitle());
question.setViewCount(dto.getViewCount());
question.setAnswerCount(dto.getAnswerCount());
question.setDateCreated(dto.getDateCreated());
questionRepository.save(question);
}
}
NOTE : The relation between Question
and Tag
are in OneToMany
you have to use Collection
type.
You have a mismatch between JSON and @Entity
structure. JSON contains numeric identifiers while the @Entity
contains actual objects representing relationships. You most likely should introduce a separate DTO class to map this JSON while in @Repository
you should load User
and Tag
objects based on their id or create new ones. You already have CascadeType.ALL
so Hibernate will cascade the persist operation.
Generally the controller layer should be separate from repository layer unless you are doing something very, very simple. This helps to evolve the service without changing the API contract eg adding new columns for auditing changes. By exposing the @Entity
as DTO you make your life harder down the road.
You should add referencedColumnName in your Child Entity Foreign Key Column
referencedColumnName="your primaray key column name"
EDIT:
referencedColumnName
Asset is Parent Entity and AssetDetails is Child Entity Here I have taken OneToOne Relationship
Asset.java
@Entity
@Table(name="asset")
public class Asset {
@Id
@GeneratedValue
@Column(name="assetid")
private BigInteger assetid;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "asset")
@JsonBackReference
private AssetDetails assetDetails;
public AssetDetails getAssetDetails() {
return assetDetails;
}
public void setAssetDetails(AssetDetails assetDetails) {
this.assetDetails = assetDetails;
assetDetails.setAsset(this);
}
public Asset(your fields, AssetDetails assetDetails) {
super();
// your fields
this.assetDetails = assetDetails;
this.assetDetails.setAsset(this);
}
public Asset() {
super();
}
public BigInteger getAssetid() {
return assetid;
}
public void setAssetid(BigInteger assetid) {
this.assetid = assetid;
}
}
AssetDetails.java
@Entity
@Table(name="assetDetails")
public class AssetDetails {
@Id
@GeneratedValue
private BigInteger assetdetailid;
@JoinColumn(name = "assetid",nullable = false, updatable = false,referencedColumnName="assetid")
@OneToOne(cascade=CascadeType.ALL)
@JsonManagedReference
private Asset asset;
public Asset getAsset() {
return asset;
}
public void setAsset(Asset asset) {
this.asset = asset;
}
public AssetDetails(your fields,Asset asset) {
super();
//your fields
this.asset = asset;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.