Привет, я пытаюсь удалить некоторые объекты из базы данных, но когда я использую @Delete, я получаю ошибку в браузерах, но Get работает. Я использую спящий JPA
Вот мои примеры кода
@ Entity
package pl.test.model;
import javax.persistence.*;
import java.util.Collection;
@Entity
public class Mestechnologygroup {
private Integer idTechnologyGroup;
private String name;
private String description;
private Integer number;
private Collection<Mestechnology> mestechnologiesByIdTechnologyGroup;
@Id
@Column(name = "idTechnologyGroup")
public Integer getIdTechnologyGroup() {
return idTechnologyGroup;
}
public void setIdTechnologyGroup(Integer idTechnologyGroup) {
this.idTechnologyGroup = idTechnologyGroup;
}
@Basic
@Column(name = "Name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "Description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Basic
@Column(name = "Number")
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Mestechnologygroup that = (Mestechnologygroup) o;
if (idTechnologyGroup != null ? !idTechnologyGroup.equals(that.idTechnologyGroup) : that.idTechnologyGroup != null)
return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (description != null ? !description.equals(that.description) : that.description != null) return false;
if (number != null ? !number.equals(that.number) : that.number != null) return false;
return true;
}
@Override
public int hashCode() {
int result = idTechnologyGroup != null ? idTechnologyGroup.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (number != null ? number.hashCode() : 0);
return result;
}
@OneToMany(mappedBy = "mestechnologygroupByIdTechnologyGroup")
public Collection<Mestechnology> getMestechnologiesByIdTechnologyGroup() {
return mestechnologiesByIdTechnologyGroup;
}
public void setMestechnologiesByIdTechnologyGroup(Collection<Mestechnology> mestechnologiesByIdTechnologyGroup) {
this.mestechnologiesByIdTechnologyGroup = mestechnologiesByIdTechnologyGroup;
}
}
presistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="testPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>pl.test.model.Mesattachmentoperation</class>
<class>pl.test.model.Mesattachmenttechnology</class>
<class>pl.test.model.Mesoperation</class>
<class>pl.test.model.Mesoperationdictionary</class>
<class>pl.test.model.Mesoperationstate</class>
<class>pl.test.model.Mesproduct</class>
<class>pl.test.model.Mesproducttype</class>
<class>pl.test.model.Mesproductxoperation</class>
<class>pl.test.model.Mesresource</class>
<class>pl.test.model.Mesresourcexoperation</class>
<class>pl.test.model.Mestechnology</class>
<class>pl.test.model.Mestechnologygroup</class>
<class>pl.test.model.Mesusers</class>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mes"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="xxxx"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
</properties>
</persistence-unit>
</persistence>
Репозиторий с методом
package pl.test.repo;
import com.sun.istack.internal.NotNull;
import pl.test.model.Mestechnologygroup;
import pl.test.model.Mesusers;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
import java.util.List;
import static javax.transaction.Transactional.TxType.REQUIRED;
import static javax.transaction.Transactional.TxType.SUPPORTS;
@Transactional(SUPPORTS)
public class TechnologyGroupRepo {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testPU");
EntityManager em = entityManagerFactory.createEntityManager();
public Mestechnologygroup find(@NotNull Integer id) {
return em.find(Mestechnologygroup.class, id);
}
public List<Mestechnologygroup> findAll() {
TypedQuery<Mestechnologygroup> query = em.createQuery("from Mestechnologygroup ", Mestechnologygroup.class);
return query.getResultList();
}
@Transactional(REQUIRED)
public void delete(@NotNull Integer id) {
em.remove(em.getReference(Mestechnologygroup.class, id));
}
}
здесь я использую @ Delete
package pl.test.rest;
import pl.test.model.Mestechnologygroup;
import pl.test.repo.TechnologyGroupRepo;
import javax.inject.Inject;
import javax.validation.constraints.Min;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.List;
import static javax.transaction.Transactional.TxType.REQUIRED;
import static javax.transaction.Transactional.TxType.SUPPORTS;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
@Path("/tg")
public class TechnologyGroupEndpoint{
@Inject
private TechnologyGroupRepo technologyGroupRepo;
@GET
@Path("/{id : \\d+}")
@Produces(APPLICATION_JSON)
public Response getBook(@PathParam("id") @Min(1) Integer id) {
Mestechnologygroup mestechnologygroup = technologyGroupRepo.find(id);
if (mestechnologygroup == null)
return Response.status(Response.Status.NOT_FOUND).build();
return Response.ok(mestechnologygroup).build();
}
@DELETE
@Path("/d/{id : \\d+}")
public Response deleteBook(@PathParam("id") @Min(1) Integer id) {
technologyGroupRepo.delete(id);
return Response.noContent().build();
}
@GET
@Produces(APPLICATION_JSON)
public Response getBooks() {
List<Mestechnologygroup> mestechnologygroups = technologyGroupRepo.findAll();
if (mestechnologygroups.size() == 0)
return Response.status(Response.Status.NO_CONTENT).build();
return Response.ok(mestechnologygroups).build();
}
}
Я отправляю ответ в Google Chrome.
Ответ
Буду признателен за любую помощь :) Заранее спасибо;)