英文:
Why I get Object references an unsaved transient instance
问题
我在两个实体之间有多对多的关系:Tag 和 Ads。当用户想要创建广告时,需要插入一些标签。但是当我尝试创建广告时,我遇到了这个错误:
{
"timestamp": "2020-10-11T21:21:44.752+0000",
"status": 500,
"error": "Internal Server Error",
"message": "org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.mybrocki.entity.Tag; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.mybrocki.entity.Tag",
"path": "/mybrocki/auth/ads/create"
}
这是我的 Tag 实体:
@Entity
@Table(name = "Tags")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@ManyToMany(mappedBy = "adsTags", cascade = {
CascadeType.ALL
})
private Set<Ads> ads;
// 省略 getter 和 setter 方法...
}
这是 Ads 实体:
@ManyToMany
@JoinTable(name = "tags_ads",
joinColumns = @JoinColumn(name = "ads_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "tag_id", referencedColumnName = "id"))
private List<Tag> adsTags;
这是服务实现类的部分代码:
ads.setVisibleAds(false);
ads.setAdsTags(new ArrayList<Tag>());
if (!adsDTO.getTags().isEmpty()) {
for (Tag tagDTO : adsDTO.getTags()) {
ads.getAdsTags().add(tagDTO);
}
}
// 其他属性的设置...
adsRepository.save(ads);
这是 JSON 数据:
{
"adsGroupId": 5,
"adsSubGroupId": 27,
"adsType": "USED",
"description": "Ford fiesta 1.3",
"fixedPrice": false,
"freeDelivery": false,
"image": [
"http://res.cloudinary.com/mybrocki/image/upload/v1589887253/s5d11rshydka5nov2wox.jpg,http://res.cloudinary.com/mybrocki/image/upload/v1589887253/s5d11rshydka5nov2wox.jpg,"
],
"price": 800,
"productName": "FOcscscsdcRD Goragjhggiujkjhkjoijiohiuhn",
"productWarranty": false,
"urgentSales": true,
"tags": [
{
"name": "Second hand"
}
]
}
可能的问题是什么?
英文:
I have many-to-many relationship between two entities: Tag and Ads. When User wants to create ad, he need to insert few tags. But when I try to create add I am getting this error:
{
"timestamp": "2020-10-11T21:21:44.752+0000",
"status": 500,
"error": "Internal Server Error",
"message": "org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.mybrocki.entity.Tag; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.mybrocki.entity.Tag",
"path": "/mybrocki/auth/ads/create"
}
This is my Tag entity:
@Entity
@Table(name="Tags")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="name")
private String name;
@ManyToMany(mappedBy = "adsTags", cascade = {
CascadeType.ALL
})
private Set<Ads> ads;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Ads> getAds() {
return ads;
}
public void setAds(Set<Ads> ads) {
this.ads = ads;
}
And this is Ads entity:
@ManyToMany
@JoinTable(name = "tags_ads",
joinColumns = @JoinColumn(name = "ads_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "tag_id", referencedColumnName = "id"))
private List<Tag> adsTags;
And this is service implementation class:
ads.setVisibleAds(false);
ads.setAdsTags(new ArrayList<Tag>());
if (!adsDTO.getTags().isEmpty()) {
for (Tag tagDTO : adsDTO.getTags()) {
ads.getAdsTags().add(tagDTO); }
}
ads.setStatus(AdsStatus.READYFORREVIEW.toString());
ads.setAdssubgroup(adsSubGroupRepository.findOneById(adsDTO.getAdsSubGroupId()));
ads.setFixedPrice(adsDTO.getFixedPrice());
ads.setFreeDelivery(adsDTO.getFreeDelivery());
ads.setProductWarranty(adsDTO.getProductWarranty());
ads.setUrgentSales(adsDTO.getUrgentSales());
Date currentDate = new Date();
Instant instant = currentDate.toInstant();
LocalDateTime ldt = instant.atOffset(ZoneOffset.UTC).toLocalDateTime();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
adsDTO.setAdsDate(currentDate);
ads.setAdsDate(adsDTO.getAdsDate());
adsRepository.save(ads);
This is json:
{
"adsGroupId": 5,
"adsSubGroupId": 27,
"adsType": "USED",
"description": "Ford fiesta 1.3",
"fixedPrice": false,
"freeDelivery": false,
"image": [
"http://res.cloudinary.com/mybrocki/image/upload/v1589887253/s5d11rshydka5nov2wox.jpg,http://res.cloudinary.com/mybrocki/image/upload/v1589887253/s5d11rshydka5nov2wox.jpg,"
],
"price": 800,
"productName": "FOcscscsdcRD Goragjhggiujkjhkjoijiohiuhn",
"productWarranty": false,
"urgentSales": true,
"tags": [
{
"name":"Second hand"
}]
}
What could be the problem?
答案1
得分: 2
你的 Tag
类上有 cascade = {CascadeType.ALL }
,但是你的 Ads
类上却没有,如果你想保存你的 Ads
对象并级联保存其中的 Tag
对象,那么你需要在 Ads
类的字段上加上这个注解。否则,请保存 Tag
对象而不是 Ads
对象。
英文:
You have cascade = {CascadeType.ALL }
on your Tag
class but not on your Ads
class, if you want to save your Ads
object and cascade save the Tag
objects in it, then you need the annotation on the field of your Ads
class. Otherwise save the Tag
object instead of the Ads
object.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论