Testando classes de entidade jpa - erro A transação é necessária
Baseado em umarquétipo eu criei um aplicativo java ee. Há um teste arquillian incluído que funciona bem. apenas chama um método em um bean @Stateless que persiste em uma entidade pré-fabricada.
agora eu adicionei alguma entidade com algumas relações e escrevi um teste para elas. Mas ao perecer qualquer entidade que recebo
Transaction is required to perform this operation (either use a transaction or extended persistence context)
Eu acho que preciso marcar o método de teste com @Transactional, mas parece não estar no caminho da classe. A chamada manual da transação no EntityManager injetado gera outro erro. Então, como configurar corretamente esses testes e dependências.
EDITAR Como Grzesiek D. sugeriu, aqui estão alguns detalhes. esta é a entidade (a que une outras):
@Entity
public class Booking implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* internal id.
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
/**
* Used for optimistic locking.
*/
@Version
@Column(name = "version")
private int version;
/**
* A booking must have a project related.
*/
@ManyToOne
@JoinColumn(name = "project_id")
@NotNull
private Project project;
/**
* A booking must have an owner.
*/
@ManyToOne
@JoinColumn(name = "user_id")
@NotNull
private User owner;
/**
* A booking always has a start time.
*/
@Column
@NotNull
private Timestamp start;
/**
* A booking always has an end time.
*/
@Column
@NotNull
private Timestamp end;
/**
*
* @return true if start is befor end. false otherwise (if equal or after end).
*/
@AssertTrue(message = "Start must before end.")
public final boolean isStartBeforeEnd() {
return start.compareTo(end) < 0;
}
/**
* @return the id
*/
public final Long getId() {
return id;
}
/**
* @param id
* the id to set
*/
public final void setId(final Long id) {
this.id = id;
}
/**
* @return the version
*/
public final int getVersion() {
return version;
}
/**
* @param version
* the version to set
*/
public final void setVersion(final int version) {
this.version = version;
}
/**
* @return the project
*/
public final Project getProject() {
return project;
}
/**
* @param project
* the project to set
*/
public final void setProject(final Project project) {
this.project = project;
}
/**
* @return the owner
*/
public final User getOwner() {
return owner;
}
/**
* @param owner
* the owner to set
*/
public final void setOwner(final User owner) {
this.owner = owner;
}
/**
* @return the start
*/
public final Timestamp getStart() {
return start;
}
/**
* @param start
* the start to set
*/
public final void setStart(final Timestamp start) {
this.start = start;
}
/**
* @return the end
*/
public final Timestamp getEnd() {
return end;
}
/**
* @param end
* the end to set
*/
public final void setEnd(final Timestamp end) {
this.end = end;
}
//hashCode, equals, toString omitted here
}
Aqui está o teste:
@RunWith(Arquillian.class)
public class BookingTest {
@Deployment
public static Archive<?> createDeployment() {
return ArquillianContainer.addClasses(Resources.class, Booking.class, Project.class, User.class);
}
@Inject
private EntityManager em;
@Test
public void createBooking() {
Booking booking = new Booking();
booking.setStart(new Timestamp(0));
booking.setEnd(new Timestamp(2));
User user = new User();
user.setName("Klaus");
booking.setOwner(user);
Project project = new Project();
project.setName("theOne");
project.setDescription("blub");
booking.setProject(project);
em.persist(booking);
System.out.println("here");
}
}
E aqui a exceção:
javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context)
Eu sei que funcionará se eu criar um bean @Stateless e encapsular a persistência lá, mas quero um teste direto da validação da entidade e preciso de um playground para evoluir o modelo de dados.