IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Web Java Discussion :

Sring-MVC + JPA = EntityManager null


Sujet :

Spring Web Java

  1. #1
    Membre confirmé
    Inscrit en
    Février 2010
    Messages
    101
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 101
    Par défaut Sring-MVC + JPA = EntityManager null
    Bonjour,

    je me suis mis à Spring MVC et j'essaye d'attaquer une base de donnée avec JPA sur un serveur TOMCAT.

    Lorsque je lance mon tomcat (embarqué dans maven avec le plugin), mon entityManager reste null et je sais pas pourquoi.... Je vous montre le dév réalisé jusqu'a maintenant :

    Mon entity :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    @Entity
    @Table(name = "cra_Form")
    public class FormEntity implements Serializable {
     
    	/** long serialVersionUID. */
    	private static final long serialVersionUID = 1L;
    	/** Integer idForm. */
    	// , insertable = false, updatable = false, nullable = false, unique = true
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	@Column(name = "idForm")
    	private Integer idForm;
    	/** String title. */
    	@Column(name = "title")
    	private String title;
    	/** String typeCra. */
    	@Column(name = "typeCra", nullable = false)
    	private String typeCra;
     
    	/**
             * FormEntity constructor.
             */
    	public FormEntity() {
    		super();
    	}
     
    	/**
             * getIdForm
             * 
             * @return Integer
             */
    	public Integer getIdForm() {
    		return idForm;
    	}
     
    	/**
             * setIdForm
             * 
             * @param _idForm Integer
             */
    	public void setIdForm(Integer _idForm) {
    		this.idForm = _idForm;
    	}
     
    	/**
             * getTitle
             * 
             * @return String
             */
    	public String getTitle() {
    		return title;
    	}
     
    	/**
             * setTitle
             * 
             * @param _title String
             */
    	public void setTitle(String _title) {
    		this.title = _title;
    	}
     
    	/**
             * getTypeCra
             * 
             * @return typeCra String
             */
    	public String getTypeCra() {
    		return typeCra;
    	}
     
    	/**
             * setTypeCra
             * 
             * @param _typeCra String
             */
    	public void setTypeCra(String _typeCra) {
    		this.typeCra = _typeCra;
    	}
     
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see java.lang.Object#hashCode()
    	 */
    	@Override
    	public int hashCode() {
    		return this.getIdForm().hashCode();
    	}
     
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see java.lang.Object#equals(java.lang.Object)
    	 */
    	@Override
    	public boolean equals(Object _obj) {
    		if (this == _obj) {
    			return true;
    		}
    		if (_obj == null) {
    			return false;
    		}
    		if (!(_obj instanceof FormEntity)) {
    			return false;
    		}
    		FormEntity pk = (FormEntity) _obj;
    		return pk.hashCode() == this.hashCode();
    	}
     
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see java.lang.Object#toString()
    	 */
    	public String toString() {
    		final String TAB = "    ";
     
    		StringBuffer retValue = new StringBuffer();
     
    		retValue.append("FormEntity ( ").append(TAB).append("idForm = ").append((this.idForm != null) ? this.idForm : "null").append(TAB).append("title = ")
    				.append((this.title != null) ? this.title : "null").append(TAB).append("typeCra = ").append((this.typeCra != null) ? this.typeCra : "null")
    				.append(TAB).append(" )");
     
    		return retValue.toString();
    	}
    }
    Le AbstractDAO:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    @Repository
    public abstract class AbstractDAO<T> {
     
    	@PersistenceContext(type = PersistenceContextType.EXTENDED)
    	private EntityManager entityManager;
     
    	/**
             * getEntityManager
             *
             * @return entityManager
             */
    	public EntityManager getEntityManager() {
    		return entityManager;
    	}
     
    	/**
             * setEntityManager
             *
             * @param _entityManager
             */
    	public void setEntityManager(EntityManager _entityManager) {
    		this.entityManager = _entityManager;
    	}
     
    	/**
             * create.
             * 
             * @param _obj T
             * @return T
             * @throws DAOException
             */
    	@Transactional
    	public abstract T create(T _obj) throws DAOException;
     
    	/**
             * update.
             * 
             * @param _obj T
             * @return T
             * @throws DataAccessObjectException
             */
    	@Transactional
    	public abstract T update(T _obj) throws DAOException;
     
    	/**
             * delete.
             * 
             * @param _obj T
             * @throws DataAccessObjectException
             */
    	@Transactional
    	public abstract void delete(T _obj) throws DAOException;
     
    	/**
             * getReference.
             * 
             * @param _obj T
             * @return T
             * @throws DataAccessObjectException
             */
    	@Transactional
    	public abstract T getReference(T _obj) throws DAOException;
     
    	/**
             * findByKey.
             * 
             * @param _obj T
             * @return T
             * @throws DataAccessObjectException
             */
    	@Transactional
    	public abstract T findByKey(T _obj) throws DAOException;
    }
    Le DAO :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    @Repository
    public class FormDAO extends AbstractDAO<FormEntity> {
     
    	/** Logger LOGGER */
    	private static final Logger LOGGER = LoggerFactory.getLogger(FormDAO.class);
     
    	/**
             * FormDAO constructor.
             */
    	public FormDAO() {
    	}
     
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see com.test.cra.persistence.dao.AbstractDAO#create(java.lang.Object)
    	 */
    	@Override
    	public FormEntity create(FormEntity _formEntity) throws DAOException {
    		try {
    			LOGGER.debug("create(FormEntity _formEntity=" + _formEntity.toString() + ") - start");
    			this.getEntityManager().persist(_formEntity);
    			this.getEntityManager().flush();
    			this.getEntityManager().refresh(_formEntity);
    			LOGGER.debug("create(FormEntity) - end");
    			return _formEntity;
    		} catch (NullPointerException e) {
    			throw new DAOException(MSGError.NULLPOINTER, e);
    		} catch (EntityExistsException e) {
    			throw new DAOException(MSGError.ENTITYEXISTS, e);
    		} catch (IllegalStateException e) {
    			throw new DAOException(MSGError.ILLEGALSTATE, e);
    		} catch (IllegalArgumentException e) {
    			throw new DAOException(MSGError.ILLEGALARGUMENT, e);
    		} catch (TransactionRequiredException e) {
    			throw new DAOException(MSGError.TRANSACTIONREQUIRED, e);
    		}
    	}
     
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see com.test.cra.persistence.dao.AbstractDAO#update(java.lang.Object)
    	 */
    	@Override
    	public FormEntity update(FormEntity _formEntity) throws DAOException {
    		try {
    			LOGGER.debug("update(FormEntity _formEntity=" + _formEntity + ") - start");
    			FormEntity formEntity = this.getEntityManager().merge(_formEntity);
    			this.getEntityManager().flush();
    			LOGGER.debug("update(FormEntity) - end");
    			return formEntity;
    		} catch (NullPointerException e) {
    			throw new DAOException(MSGError.NULLPOINTER, e);
    		} catch (IllegalStateException e) {
    			throw new DAOException(MSGError.ILLEGALSTATE, e);
    		} catch (TransactionRequiredException e) {
    			throw new DAOException(MSGError.TRANSACTIONREQUIRED, e);
    		}
    	}
     
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see com.test.cra.persistence.dao.AbstractDAO#delete(java.lang.Object)
    	 */
    	@Override
    	public void delete(FormEntity _formEntity) throws DAOException {
    		try {
    			LOGGER.debug("delete(FormEntity _formEntity=" + _formEntity + ") - start");
    			this.getEntityManager().remove(_formEntity);
    			this.getEntityManager().flush();
    			LOGGER.debug("delete(FormEntity) - end");
    		} catch (NullPointerException e) {
    			throw new DAOException(MSGError.NULLPOINTER, e);
    		} catch (IllegalStateException e) {
    			throw new DAOException(MSGError.ILLEGALSTATE, e);
    		} catch (TransactionRequiredException e) {
    			throw new DAOException(MSGError.TRANSACTIONREQUIRED, e);
    		}
    	}
     
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see com.test.cra.persistence.dao.AbstractDAO#getReference(java.lang.Object)
    	 */
    	@Override
    	public FormEntity getReference(FormEntity _formEntity) throws DAOException {
    		try {
    			LOGGER.debug("getReference(FormEntity _formEntity=" + _formEntity + ") - start");
    			FormEntity formEntity = this.getEntityManager().getReference(FormEntity.class, _formEntity.getIdForm());
    			LOGGER.debug("getReference(FormEntity) - end");
    			return formEntity;
    		} catch (NullPointerException e) {
    			throw new DAOException(MSGError.NULLPOINTER, e);
    		} catch (IllegalStateException e) {
    			throw new DAOException(MSGError.ILLEGALSTATE, e);
    		} catch (IllegalArgumentException e) {
    			throw new DAOException(MSGError.ILLEGALARGUMENT, e);
    		}
    	}
     
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see com.test.cra.persistence.dao.AbstractDAO#findByKey(java.lang.Object)
    	 */
    	@Override
    	public FormEntity findByKey(FormEntity _formEntity) throws DAOException {
    		try {
    			LOGGER.debug("findByKey(FormEntity _formEntity=" + _formEntity + ") - start");
    			FormEntity formEntity = this.getEntityManager().find(FormEntity.class, _formEntity.getIdForm());
    			LOGGER.debug("findByKey(FormEntity) - end");
    			return formEntity;
    		} catch (NullPointerException e) {
    			throw new DAOException(MSGError.NULLPOINTER, e);
    		} catch (IllegalStateException e) {
    			throw new DAOException(MSGError.ILLEGALSTATE, e);
    		} catch (IllegalArgumentException e) {
    			throw new DAOException(MSGError.ILLEGALARGUMENT, e);
    		}
    	}
     
    	/**
             * findByTypeCRA
             * 
             * @param _typeCra String
             * @return FormEntity
             * @throws DAOException
             */
    	@Transactional
    	public FormEntity findByTypeCRA(String _typeCra) throws DAOException {
    		try {
    			LOGGER.debug("findByTypeCRA(String _typeCra=" + _typeCra + ") - start");
    			FormEntity formEntity = null;
    			Query query = this.getEntityManager().createQuery("SELECT OBJECT(formEntity) FROM FormEntity formEntity WHERE formEntity.typeCra = :typeCra");
    			query.setParameter("typeCra", _typeCra);
    			List<?> listQuery = query.getResultList();
    			if (listQuery != null && listQuery.get(0) != null) {
    				formEntity = (FormEntity) listQuery.get(0);
    			}
    			LOGGER.debug("findByTypeCRA(String) - end");
    			return formEntity;
    		} catch (EntityNotFoundException e) {
    			throw new DAOException(MSGError.NORESULT, e);
    		} catch (NullPointerException e) {
    			throw new DAOException(MSGError.NULLPOINTER, e);
    		} catch (IllegalStateException e) {
    			throw new DAOException(MSGError.ILLEGALSTATE, e);
    		}
    	}
    }
    le persistence.xml

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <?xml version="1.0"  encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
     
     
    	<persistence-unit name="puSqlServer" transaction-type="RESOURCE_LOCAL">
    		<class>com.test.cra.persistence.entity.CraEntity</class>
    		<class>com.test.cra.persistence.entity.FormEntity</class>
    		<class>com.test.cra.persistence.entity.FormQuestionEntity</class>
    		<class>com.test.cra.persistence.entity.FormQuestionEntityPK</class>
    		<class>com.test.cra.persistence.entity.QuestionEntity</class>
    		<class>com.test.cra.persistence.entity.QuestionResponseEntity</class>
    		<class>com.test.cra.persistence.entity.QuestionResponseEntityPK</class>
    		<class>com.test.cra.persistence.entity.ResponseEntity</class>
    	</persistence-unit>
    </persistence>
    l'applicationContext.xml

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    41
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Contexte d'application pour spring-mvc-webapp. -->
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
    	xmlns="http://www.springframework.org/schema/beans"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                              http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
     
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    	    p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    		p:url="jdbc:sqlserver://192.168.16.40;DatabaseName=cra;"
    		p:username="usercra"
    		p:password="usercra" />
     
    	<bean id="entityManagerFactory"
    		  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    		  p:dataSource-ref="dataSource"
    		  p:persistence-unit-name="puSqlServer">
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
    				p:databasePlatform="org.hibernate.dialect.SQLServerDialect"
    				p:showSql="true"
    				p:generate-ddl="true" />
    		</property>
    	</bean>
     
    	<bean id="transactionManager"
    		  class="org.springframework.orm.jpa.JpaTransactionManager"
    		  p:entityManagerFactory-ref="entityManagerFactory" />
     
    	<context:annotation-config />
     
    	<context:component-scan base-package="com.test.cra" />
     
    	<tx:annotation-driven />
    </beans>
    J'espère que j'ai mis tout ce qu'il faut, n'hésitez pas si vous avez besoin d'autre chose.

    Pour le moment, je suis perdu, je ne vois pas trop ce qu'il manque.

    Merci de votre aide!!

  2. #2
    Membre confirmé
    Inscrit en
    Février 2010
    Messages
    101
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 101
    Par défaut
    Personne aurait une petite idée??

  3. #3
    Membre confirmé
    Inscrit en
    Février 2010
    Messages
    101
    Détails du profil
    Informations forums :
    Inscription : Février 2010
    Messages : 101
    Par défaut
    J'ai résolu mon problème en utilisant l'annotation @Autowired sur l'instanciation de mes services ainsi que de mes DAO!

    Merci!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. EJB3 EntityManager null
    Par lilmeth dans le forum Java EE
    Réponses: 1
    Dernier message: 05/01/2011, 14h34
  2. [Data] Spring MVC + JPA
    Par kaouther16 dans le forum Spring
    Réponses: 0
    Dernier message: 11/08/2009, 14h59
  3. EntityManager, null pointer exception.
    Par FinalSpirit dans le forum JPA
    Réponses: 7
    Dernier message: 07/07/2009, 10h29
  4. [débutant] EntityManager : null pointer exception
    Par mateu34 dans le forum Glassfish et Payara
    Réponses: 3
    Dernier message: 16/02/2009, 21h58
  5. [EJB3 Entity] EntityManager null
    Par pmartin8 dans le forum Java EE
    Réponses: 2
    Dernier message: 29/04/2007, 16h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo