Olá Otavio,
Olhando depois o codigo disponibilizado pelo instrutor do curso, ele transformou a classe LivroDao em EJB utilizando o @Statefull e @PersistenceContext(type = PersistenceContextType.EXTENDED). O uso do Join fetch não ja resolveria a questao de carregar os livros e os autores na mesma sessao do hibernate?
Segue as Livro e o Pom que vc pediu. Lembrando que o projeto é de JSF
@Entity
@Cacheable //A entidade toda sera cacheada com os Selects do LivroDao
@XmlRootElement // Essa anotação indica que o valor da classe será representado como um elemento XML principal.
@XmlAccessorType(XmlAccessType.FIELD) //informa que queremos o acesso através do campo (field):
public class Livro {
//Pode ser usado os beanValidator conforme as anotações abaixo. Está configurado no jsf_messages.properties
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotBlank
private String titulo;
@Lob // avisa pro mysql que podera receber mto texto
@NotBlank
@Length(min=10, max=1000)
private String descricao;
@DecimalMin("20")
private BigDecimal preco;
@Min(50)
private Integer numeroPaginas;
@ManyToMany
@Size(min=1)
@NotNull
@XmlElement(name="autor")
@XmlElementWrapper(name="autores")
private List<Autor> autores = new ArrayList<>();
@Temporal(TemporalType.DATE)
private Calendar dataPublicacao;
private String capaPath;
//getters e setters
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.alura</groupId>
<artifactId>casadocodigo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<finalName>casadocodigo</finalName>
</build>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>3.0.3.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.spec.javax.faces</groupId>
<artifactId>jboss-jsf-api_2.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.6.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.6.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.omnifaces</groupId>
<artifactId>omnifaces</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
</project>