引导,Jpa和hibernate在场地和事件之间保持一对多的关系。
我正在检索的错误是
org.h2.jdbc.JdbcSQLException: NULL not allowed for column "VENUE_LOCATION"; SQL statement:
insert into event (id, date, description, media_ref, num_ratings, performer, performer_media, title, total_rating) values (null, ?, ?, ?, ?, ?, ?, ?, ?) [23502-192]
我已经尝试首先保存父(Venue)类,但是会产生相同的错误。
地点
public class Venue
{
@Id
private String name;
@Id
private String location;
@OneToOne(mappedBy = "venue",cascade = CascadeType.ALL)
@JoinColumn(name="id")
private VenueUser venueUser;
private String mediaRef;
private int rating;
@OneToMany(fetch=FetchType.LAZY,mappedBy = "venue",cascade = CascadeType.ALL)
private Set<Event> events;
//Constructors getters and setters below
事件
@Entity
public class Event
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String title;
private String description;
private String performer;
private String[] performerMedia;
private Calendar[] date;
@Transient
private double avgRating;
private int numRatings;
private int totalRating;
private String mediaRef;
@MapsId("name")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="Venue_name",referencedColumnName = "name"),
@JoinColumn(name="venue_location",referencedColumnName = "location")
})
private Venue venue;
//Constructors getters and setters below
控制器
@RequestMapping(value = "/event",method=RequestMethod.POST)
public ResponseEntity addEvent(@RequestBody Event event)
{
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName(); //get logged in username
Venue venue = userVenueRepository.findByEmail(name).getVenue();
event.setVenue(venue);
venue.addEvent(event);
if(eventRepository.saveAndFlush(event).equals(event)&&venueRepository.saveAndFlush(venue).equals(venue))
{
return new ResponseEntity(HttpStatus.CREATED);
}
else
{
return new ResponseEntity(HttpStatus.CONFLICT);
}
}
答案 0 :(得分:0)
$ $env:TZ='UTC'
$ node
> new Date().toString()
'Fri Sep 02 2016 16:15:34 GMT+0000 (GMT Standard Time)'
您需要为您的Event实体设置ID。最好使用insert into event (id, date, description, media_ref, num_ratings, performer, performer_media, title, total_rating) values (null, ?, ?, ?, ?, ?, ?, ?, ?)
@GeneratedValue
注释,例如https://github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java
或使用AbstractPersistable类作为父实体。
答案 1 :(得分:0)
错误说Venue实体的location
字段为空,不能是主键。
您有两个持久化Event对象的选项。
cascade = CascadeType.PERSIST
,然后保存子级实体。答案 2 :(得分:0)
好的,所以我设法修复了这个并且事后我不应该盲目地遵循教程,我不确定@MapsId做了什么所以我删除它并且一切都开始工作。如果有人能解释什么是@MapsId这是为什么以及为什么会引起一些值得赞赏的问题。
答案 3 :(得分:0)
这样,您无需在子对象中添加父项。
通过表单Venue
实体删除mapp
然后在Venue
之前的Set<Event>
实体内部添加以下代码
@JoinColumns({
@JoinColumn(name="Venue_name",referencedColumnName = "name"),
@JoinColumn(name="venue_location",referencedColumnName = "location")
})
从@JoinColumns
实体中删除@MapsId
和Event
那你就不需要写
event.setVenue(venue);
希望有帮助。