{
"name" : "SC1",
"address" : "Street 1111",
"description" : "A Sport center",
"email" : "sc@sc.com",
"phones" : [{"number" : "123456"}, {"number" : "654321"}],
"services" : [{"idService" : 1}, {"idService" : 2}],
"commune" : {"idCommune" : 1},
"users" : [{"idUser":62}]
}
2018-06-25 03:56:32.670 DEBUG 20696 --- [nio-8080-exec-6] org.hibernate.SQL : insert into sport_center (address, Commune_idCommune, description, email, name) values (?, ?, ?, ?, ?)
2018-06-25 03:56:32.671 TRACE 20696 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [Street 1111]
2018-06-25 03:56:32.671 TRACE 20696 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [INTEGER] - [1]
2018-06-25 03:56:32.671 TRACE 20696 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [VARCHAR] - [A Sport center]
2018-06-25 03:56:32.671 TRACE 20696 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [VARCHAR] - [sc@sc.com]
2018-06-25 03:56:32.671 TRACE 20696 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [VARCHAR] - [SC1]
2018-06-25 03:56:32.800 DEBUG 20696 --- [nio-8080-exec-6] org.hibernate.SQL : insert into phone (number, Sport_center_idSport_Center) values (?, ?)
2018-06-25 03:56:32.800 TRACE 20696 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [123456]
2018-06-25 03:56:32.800 TRACE 20696 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [INTEGER] - [252]
2018-06-25 03:56:32.942 DEBUG 20696 --- [nio-8080-exec-6] org.hibernate.SQL : insert into phone (number, Sport_center_idSport_Center) values (?, ?)
2018-06-25 03:56:32.942 TRACE 20696 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [654321]
2018-06-25 03:56:32.942 TRACE 20696 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [INTEGER] - [252]
2018-06-25 03:56:33.368 ERROR 20696 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.eiffel.canchai.model.Service; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.eiffel.canchai.model.Service] with root cause
org.hibernate.PersistentObjectException: detached entity passed to persist: com.eiffel.canchai.model.Service
我的课程如下:
sportcenter.java
@Entity
@Table(name = "sport_center")
public class SportCenter implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idSport_Center")
private Integer idSportCenter;
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@Column(name = "description")
private String description;
@Column(name = "email")
private String email;
@JoinTable(name = "sport_center_has_user", joinColumns = {
@JoinColumn(name = "Sport_center_idSport_Center", referencedColumnName = "idSport_Center")}, inverseJoinColumns = {
@JoinColumn(name = "User_idUser", referencedColumnName = "idUser")})
@ManyToMany(cascade = CascadeType.ALL)
private List<User> users;
@JoinTable(name = "sport_center_has_service", joinColumns = {
@JoinColumn(name = "Sport_center_idSport_Center", referencedColumnName = "idSport_Center")}, inverseJoinColumns = {
@JoinColumn(name = "Service_idService", referencedColumnName = "idService")})
@ManyToMany(cascade = CascadeType.ALL)
private List<Service> services;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "sportCenter")
@JsonIgnore
private List<ImageField> imageFields;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "sportCenter")
@JsonIgnore
private List<Field> fields;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "sportCenter", fetch = FetchType.LAZY)
private List<Phone> phones;
@JoinColumn(name = "Commune_idCommune", referencedColumnName = "idCommune")
@ManyToOne(optional = false)
private Commune commune;
public SportCenter() {
}
service.java
@Entity
@Table(name = "service")
public class Service implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idService")
private Integer idService;
@Column(name = "name")
private String name;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "services", cascade = CascadeType.ALL)
@JsonIgnore
private List<SportCenter> sportCenters;
public Service() {
}
@Repository
@Transactional
public class ServiceDao implements IServiceDao {
@PersistenceContext
private EntityManager entityManager;
@Override
public void save(Service entity) {
entityManager.persist(entity);
}
@Controller
@RequestMapping("/sportcenter")
@CrossOrigin
public class SportCenterController {
@Autowired
private ISportCenterService sportCenterService;
@PostMapping("/register")
public ResponseEntity<?> registerSportCenter(@RequestBody SportCenter sc){
if (sc.getEmail().length() == 0 || sc.getName().length() == 0 || sc.getAddress().length() == 0 || sc.getPhones().size() == 0 || sc.getServices().size() == 0) {
return new ResponseEntity(new ErrorMsg("Check parameters. One of them is missing"),HttpStatus.NO_CONTENT);
}
for (Phone p : sc.getPhones()) {
p.setSportCenter(sc);
}
for (Service s : sc.getServices()) {
s.addSportCenters(sc);
}
sportCenterService.save(sc);
return new ResponseEntity(HttpStatus.OK);
}
提前道谢!
这里的问题是,您正在将persist事件从父级级联到子级。
当子实体具有非零ID时,hibernate将假定它已经存在。因为您是从rest服务获取数据,所以我猜子“服务”实体没有分离。因此出现错误“已分离的实体传递到持久”。
您需要首先持久化父实体,通过合并调用管理子实体,然后将父实体链接到子实体,或者删除cascade.all。
初始数据。专业有很多科目。 专业JAVA 对于一个主题来说,专业不能为空。我希望它能以下一种方式工作:当我保存/更新/分离一个主题时,同样的操作必须应用于专业。当我删除一个主题时,专业不会发生任何变化。 主题JAVA 我写了一个集成测试。生成专业()和生成主题()这只是util方法。 你可以在这张图片上看到测试结果。 subjectService在此处引发了异常。保存(主题1); 原因:org。冬
我最近开始为一个学校项目与JPA和Hibernate合作,它基本上在数据库中存储大陆、国家和城市。 当尝试在实体上使用持久化方法时,我得到了标题中提到的错误。 我访问过的任何一个线程都解决了许多更复杂的问题,但对我的情况没有真正的帮助。我希望通过在这里发帖找到一些指导。 这是数据库创建脚本。这是一个PostgreSQL数据库。 基本上,countries表使用continents表的ID作为外键,
我编写了第一个java应用程序来读取rss流,并使用,,。我的模特<代码>RssFeed: : 和: 我使用处理数据。如果不使用多对多功能保存RssFeed,则可以: 但当我加上一句话: 获取异常:。 我的RssFeedServiceImpl: } 和RssCategoryServiceImpl: 拯救多少对多少?
我在spring boot尝试用hibernate进行多对一的单向映射。我有以下几个学生班--很多 大学-一个班级 应用程序另存和运行为 但我的原因是:org.hibernate.PersistentObjectException:传递给Persisted的分离实体:jpa.many.to.oniredirectoral.model.university(位于org.hibernate.event
当试图在web应用程序上创建课程实体实例时,我们遇到以下错误。这是一个JHipster应用程序完整的错误日志,下面是,但主要错误是 更新的代码:
问题内容: 我已经成功用hibernate写了我的第一个主要的孩子例子。几天后,我再次使用它并升级了一些库。不知道我做了什么,但是我再也无法使它运行了。有人可以帮助我找出返回以下错误消息的代码中的错误吗: hibernate映射: 编辑: InvoiceManager.java 发票.java InvoiceItem.java 编辑: 从客户端发送的JSON对象: 编辑: 一些详细信息: 我试图通