Junit / Spring / Jpa (peu être JNDI?)
Bonjour,
Je débute dans tout ce domaine et j'ai un problème à faire mes tests unitaires.
J'ai trouvé de nombreux exemples sur internet mais aucun que j'arrive à faire fonctionner avec mon architecture.
Je vous met mon architecture simplifié (que je n'ai pas le droit de modifier) :
COMPTE est dans une BDD X
PERSONNE est dans une BDD Y
Donc j'ai deux persistance unit !
Voici mon code :
Entites :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
@Entity
@Table(name = "COMPTE")
public class Compte {
@Id
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
@XmlTransient
private long id;
@Column(name = "LOGIN", length = 15, nullable = false)
long login;
// Getter et Setter
}
@Entity
@Table(name = "PERSONNE")
public class Personne {
@Id
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
@XmlTransient
private long id;
@Column(name = "NOM", length = 15, nullable = false)
String nom;
// Getter et Setter
} |
Les dao :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public class CompteDaoImpl {
@PersistenceContext(unitName = "persistence_x")
protected EntityManager entityManagerCompte;
public void persist(E entity) {
LOG.debug("persisting instance");
try {
getEntityManager().persist(entity);
LOG.debug("persist successful");
} catch (RuntimeException re) {
LOG.error("persist failed", re);
throw new DaoException(re);
}
}
// Getter et Setter
} |
Service :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
@Service
@Transaction
public class service {
public get testPersonne(Long login,String nom) {
// Persist du Compte
// Persist de la Personne
}
public getPersonneValide() {
getPersonneDao.getPersonneValide();
}
// Getter et Setter des dao (pour spring)
} |
persistence.xml :
Code:
1 2 3 4 5 6 7 8 9 10
|
<persistence-unit name="persistence_x"
transaction-type="RESOURCE_LOCAL">
<class>compte</class>
</persistence-unit>
<persistence-unit name="persistence_y"
transaction-type="RESOURCE_LOCAL">
<class>personne</class>
</persistence-unit> |
Et pour finir mon fichier spring.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
<bean id="persistenceUnitManager"
class="fr.PersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="entityManagerCompte"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_x" />
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>
<bean id="entityManagerPersonne"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_y" />
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:/TransactionManager" />
<property name="userTransactionName" value="UserTransaction" />
</bean>
<tx:jta-transaction-manager />
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager" /> |
En production ça marche très bien aucun soucis, en Junit ça ne marche pas
Soit je déclare un jndi et il n'arrive pas à l'initialiser, soit je crée des TransactionsManager pour chaque mais je vois pas comment faire...
En gros je suis perdu !