I am trying to set up a basic Quarkus Hibernate application to persist a simple Film entity to the Sakila test database. I've been following the guide https://quarkus.io/guides/hibernate-orm but the injection of an EntityManager
in my FilmService
returns null
no matter what I try. Most parts of the code have been used with no issues in other non-Quarkus Hibernate applications, e.g. the Entity beans Actor
and Film
.
Appreciate any advice on how to properly get hold of an EntityManager
.
application.properties
quarkus.datasource.db-kind = mysql
quarkus.datasource.username = root
quarkus.datasource.password = sakila
quarkus.datasource.jdbc.url = jdbc:mysql://localhost:3306/sakila
It should be noted that I have tested my database config in a separate Hibernate/Panache application to confirm nothing is wrong with the database connection.
Main.class
package com.example;
public class Main {
public static void main(String[] args) {
FilmService filmService = new FilmService();
filmService.createFilm("Sagan om ringen");
}
}
FilmService.class
package com.example;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
@ApplicationScoped public class FilmService {
@Inject EntityManager em;
@Transactional
public void createFilm(String giftDescription) {
Film film = new Film();
film.setFilmId((short) 12312);
film.setTitle(giftDescription);
if (em != null) {
em.persist(film);
} else {
System.out.println("EM is null!!");
}
}
}
Film.class
package com.example;
import javax.persistence.*;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;
@Entity
public class Film {
@Id
@Column(name = "film_id", columnDefinition = "SMALLINT UNSIGNED")
private short filmId;
@Basic
@Column(name = "title", columnDefinition = "VARCHAR(255)")
private String title;
@Basic
@Column(name = "description", columnDefinition = "TEXT")
private String description;
@Basic
@Column(name = "rental_duration", columnDefinition = "YEAR")
private short rentalDuration;
@Basic
@Column(name = "language_id", columnDefinition = "SMALLINT UNSIGNED")
private short languageId;
@Basic
@Column(name = "rental_rate", columnDefinition = "DECIMAL(4,2)")
private BigDecimal rentalRate;
@Basic
@Column(name = "length", columnDefinition = "SMALL UNSIGNED")
private Short length;
@Basic
@Column(name = "rating", columnDefinition = "enum('G','PG','PG-13','R','NC-17')")
private String rating;
@Basic
@Column(name = "replacement_cost")
private BigDecimal replacementCost;
@Basic
@Column(name = "last_update")
private Timestamp lastUpdate;
public short getFilmId() {
return filmId;
}
public void setFilmId(short filmId) {
this.filmId = filmId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public short getLanguageId() {
return languageId;
}
public void setLanguageId(short languageId) {
this.languageId = languageId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public short getRentalDuration() {
return rentalDuration;
}
public void setRentalDuration(short rentalDuration) {
this.rentalDuration = rentalDuration;
}
public String getRating() { return rating; }
public void setRating(String rating) { this.rating = rating; }
public BigDecimal getRentalRate() {
return rentalRate;
}
public void setRentalRate(BigDecimal rentalRate) {
this.rentalRate = rentalRate;
}
public Short getLength() {
return length;
}
public void setLength(Short length) {
this.length = length;
}
public BigDecimal getReplacementCost() {
return replacementCost;
}
public void setReplacementCost(BigDecimal replacementCost) {
this.replacementCost = replacementCost;
}
public Timestamp getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Timestamp lastUpdate) {
this.lastUpdate = lastUpdate;
}
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(
name = "film_actor",
joinColumns = { @JoinColumn(name = "film_id") },
inverseJoinColumns = { @JoinColumn(name = "actor_id") }
)
private Set<Actor> actors = new HashSet<>();
public void addActor(Actor actor) {
actors.add(actor);
actor.getFilms().add(this);
}
public void removeActor(Actor actor) {
actors.remove(actor);
actor.getFilms().remove(this);
}
public Set<Actor> getActors() {
return this.actors;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Film film = (Film) o;
if (filmId != film.filmId) return false;
if (rentalDuration != film.rentalDuration) return false;
if (title != null ? !title.equals(film.title) : film.title != null) return false;
if (description != null ? !description.equals(film.description) : film.description != null) return false;
if (rentalRate != null ? !rentalRate.equals(film.rentalRate) : film.rentalRate != null) return false;
if (length != null ? !length.equals(film.length) : film.length != null) return false;
if (replacementCost != null ? !replacementCost.equals(film.replacementCost) : film.replacementCost != null)
return false;
if (lastUpdate != null ? !lastUpdate.equals(film.lastUpdate) : film.lastUpdate != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) filmId;
result = 31 * result (title != null ? title.hashCode() : 0);
result = 31 * result (description != null ? description.hashCode() : 0);
result = 31 * result (int) rentalDuration;
result = 31 * result (rentalRate != null ? rentalRate.hashCode() : 0);
result = 31 * result (length != null ? length.hashCode() : 0);
result = 31 * result (replacementCost != null ? replacementCost.hashCode() : 0);
result = 31 * result (lastUpdate != null ? lastUpdate.hashCode() : 0);
return result;
}
}
}
Actor.class
package com.example;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@Entity
public class Actor {
public Actor() {}
public Actor(Integer actorId) {
this.actorId = actorId;
}
public Actor(Integer actorId, String firstName, String lastName) {
this.actorId = actorId;
this.firstName = firstName;
this.lastName = lastName;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "actor_id", nullable = false, updatable = false, columnDefinition = "smallint(5)")
private Integer actorId;
@Column(name = "first_name", nullable = false, columnDefinition = "varchar(45)")
private String firstName;
@Column(name = "last_name", nullable = false, columnDefinition = "varchar(45)")
private String lastName;
@ManyToMany(mappedBy = "actors")
private Set<Film> films = new HashSet<>();
public Integer getActorId() {
return actorId;
}
public void setActorId(Integer actorId) {
this.actorId = actorId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set<Film> getFilms() {
return films;
}
@Override
public String toString() {
return "Actor{"
"actorId=" actorId
", firstName='" firstName '\''
", lastName='" lastName '\''
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Actor actor = (Actor) o;
if (!Objects.equals(actorId, actor.actorId)) return false;
if (!Objects.equals(firstName, actor.firstName)) return false;
return Objects.equals(lastName, actor.lastName);
}
@Override
public int hashCode() {
int result = actorId != null ? actorId.hashCode() : 0;
result = 31 * result (firstName != null ? firstName.hashCode() : 0);
result = 31 * result (lastName != null ? lastName.hashCode() : 0);
return result;
}
}
CodePudding user response:
You should do something like:
@QuarkusMain
public class GreetingMain implements QuarkusApplication {
@Inject
FilmService filmService;
@Override
public int run(String... args) {
filmService.createFilm("Sagan om ringen");
return 0;
}
}