
Originally Posted by
dr_pompeii
Hello
1) Could you post your Domain class definition and XML configuration?
I'm not using the spring container, so I don't have the XML configuration I think you are talking about.
About the code...
Task:
Code:
// (getters and imports ommited...)
@Entity(name="TASK")
public class Task {
@Id
@GeneratedValue
@Column(name = "TASK_ID")
private Long taskId;
@Column(name = "CREATED_ON", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date createdOn;
@Column(name = "AGENT_ID", nullable = false)
private Long agentId;
@Column(name = "DESCRIPTION")
private String description;
public Task() {
}
}
Repository interface:
Code:
public interface SDTaskRepository extends JpaRepository<Task, Long>, SDTaskRepositoryCustom {
List<Task> findByStatus(long status);
}
Custom interface and implementation:
Code:
public interface SDTaskRepositoryCustom {
List<Task> findAllTasksForUser(long userId);
}
public class SDTaskRepositoryCustomImpl extends SimpleJpaRepository<Task, Long> implements SDTaskRepositoryCustom {
private EntityManager entityManager;
@Inject
public SDTaskRepositoryCustomImpl(EntityManager entityManager) {
super(Task.class, entityManager);
this.entityManager = entityManager;
}
@Override
public List<Task> findAllTasksForUser(long userId) {
return this.entityManager.createQuery("SELECT * FROM Task t WHERE t.agentId = ?1", Task.class)
.setParameter(1, userId).getResultList();
}
}
The repository client (a test, using arquillian):
Code:
@RunWith(Arquillian.class)
//@Ignore("not working yet: No property find found for type class pt.bes.pp.domain.Task")
public class SDTaskRepositoryTest {
@Deployment
public static Archive<?> createDeployment() {
return ShrinkWrap.create(JavaArchive.class).addPackage(Task.class.getPackage()).addPackage(SDTaskRepository.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Inject
SDTaskRepository taskRepository;
@Test
public void testInsertTask() throws Exception {
Task task = new Task();
task = this.taskRepository.save(task);
Assert.assertTrue(task.getTaskId() != null);
}
@Test
public void testFindAllTasksForUser() throws Exception {
List<Task> tasks = this.taskRepository.findAllTasksForUser(4L);
Assert.assertNotNull(tasks);
Assert.assertEquals(3, tasks.size());
}
Integration with CDI:
Code:
class EntityManagerFactoryProducer {
private static final Logger LOGGER = LoggerFactory.getLogger(EntityManagerFactoryProducer.class);
@Produces
@ApplicationScoped
public EntityManagerFactory createEntityManagerFactory() {
LOGGER.warn(this.getClass() + " init");
return Persistence.createEntityManagerFactory("pp");
}
public void close(@Disposes EntityManagerFactory entityManagerFactory) {
entityManagerFactory.close();
}
}
class UnqualifiedEntityManagerProducer {
@Produces
public EntityManager createEntityManager(
EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
public void close(@Disposes EntityManager entityManager) {
entityManager.close();
}
}
2) Why you are using CDI?
... because I have a constraint in this project to use JEE6 (without the Spring container).
As a final note, this example is failing also deployed on weblogic. I'm suspecting that the problem is that, as I said in the first post, customImplementation is always null.
So far, my questions are:
- is CDI+custom implementations supported?
- are there another undocumented steps that I'm missing (like the "EntityManagerProducer" that is not mentioned in the reference docs)?
Thank you.