-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34160 from mmusgrov/18737-synch-order
Order interposed sychronizations
- Loading branch information
Showing
10 changed files
with
837 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
extensions/hibernate-orm/deployment/src/test/java/io/quarkus/narayana/quarkus/Fruit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.quarkus.narayana.quarkus; | ||
|
||
import jakarta.persistence.Cacheable; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.NamedQuery; | ||
import jakarta.persistence.QueryHint; | ||
import jakarta.persistence.SequenceGenerator; | ||
import jakarta.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "known_fruits") | ||
@NamedQuery(name = "Fruits.findAll", query = "SELECT f FROM Fruit f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true")) | ||
@Cacheable | ||
public class Fruit { | ||
@Id | ||
@SequenceGenerator(name = "fruitsSequence", sequenceName = "known_fruits_id_seq", allocationSize = 1, initialValue = 10) | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "fruitsSequence") | ||
private Integer id; | ||
|
||
@Column(length = 40, unique = true) | ||
private String name; | ||
|
||
public Fruit() { | ||
} | ||
|
||
public Fruit(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...ons/hibernate-orm/deployment/src/test/java/io/quarkus/narayana/quarkus/FruitResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package io.quarkus.narayana.quarkus; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.event.Event; | ||
import jakarta.inject.Inject; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.transaction.Transactional; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.DELETE; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.PUT; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.ExceptionMapper; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.annotations.jaxrs.PathParam; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
@Path("fruits") | ||
@ApplicationScoped | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
public class FruitResource { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(FruitResource.class.getName()); | ||
|
||
@Inject | ||
EntityManager entityManager; | ||
|
||
@Inject | ||
Event<Fruit> eventBus; | ||
|
||
@GET | ||
public List<Fruit> get() { | ||
return entityManager.createNamedQuery("Fruits.findAll", Fruit.class) | ||
.getResultList(); | ||
} | ||
|
||
@GET | ||
@Path("{id}") | ||
public Fruit getSingle(@PathParam Integer id) { | ||
Fruit entity = entityManager.find(Fruit.class, id); | ||
if (entity == null) { | ||
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404); | ||
} | ||
return entity; | ||
} | ||
|
||
@POST | ||
@Transactional | ||
public Response create(Fruit fruit) { | ||
if (fruit.getId() != null) { | ||
throw new WebApplicationException("Id was invalidly set on request.", 422); | ||
} | ||
|
||
entityManager.persist(fruit); | ||
eventBus.fire(fruit); | ||
return Response.ok(fruit).status(201).build(); | ||
} | ||
|
||
@PUT | ||
@Path("{id}") | ||
@Transactional | ||
public Fruit update(@PathParam Integer id, Fruit fruit) { | ||
if (fruit.getName() == null) { | ||
throw new WebApplicationException("Fruit Name was not set on request.", 422); | ||
} | ||
|
||
Fruit entity = entityManager.find(Fruit.class, id); | ||
|
||
if (entity == null) { | ||
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404); | ||
} | ||
|
||
entity.setName(fruit.getName()); | ||
return entity; | ||
} | ||
|
||
@DELETE | ||
@Path("{id}") | ||
@Transactional | ||
public Response delete(@PathParam Integer id) { | ||
Fruit entity = entityManager.getReference(Fruit.class, id); | ||
if (entity == null) { | ||
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404); | ||
} | ||
entityManager.remove(entity); | ||
return Response.status(204).build(); | ||
} | ||
|
||
@Provider | ||
public static class ErrorMapper implements ExceptionMapper<Exception> { | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
@Override | ||
public Response toResponse(Exception exception) { | ||
LOGGER.error("Failed to handle request", exception); | ||
|
||
int code = 500; | ||
if (exception instanceof WebApplicationException) { | ||
code = ((WebApplicationException) exception).getResponse().getStatus(); | ||
} | ||
|
||
ObjectNode exceptionJson = objectMapper.createObjectNode(); | ||
exceptionJson.put("exceptionType", exception.getClass().getName()); | ||
exceptionJson.put("code", code); | ||
|
||
if (exception.getMessage() != null) { | ||
exceptionJson.put("error", exception.getMessage()); | ||
} | ||
|
||
return Response.status(code) | ||
.entity(exceptionJson) | ||
.build(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.