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

Hibernate Java Discussion :

Null pointer environnement Java-Spring Hibernate Angular


Sujet :

Hibernate Java

  1. #1
    Futur Membre du Club
    Femme Profil pro
    Développeuse J2EE
    Inscrit en
    Mars 2015
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeuse J2EE
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2015
    Messages : 10
    Points : 7
    Points
    7
    Par défaut Null pointer environnement Java-Spring Hibernate Angular
    Bonjour,

    Alors voilà j'ai un petit souci.

    Je dois développer une évolution sur une application d'entreprise.
    En fonction d'une date récupérer en base, on doit pour empecher de télécharger un fichier.
    Je ne suis pas très à l'aise avec Spring et c'est un projet très costaud.

    Du coup j'ai essayer de développer des classes en essayant de m'adapter comme je peux à l'environnement.

    Voici la classe Autorisation

    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
    package com.biocodex.elisa.domain;
     
    import java.util.Date;
     
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
     
    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.annotation.JsonProperty;
     
    @Entity
    @Table(name = "ZELISA_BLOC_PERI")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Autorisation extends AbstractPersistable<Long>{
     
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
    	/**
             * 
             */
     
    	@Id
    	@Column(name = "PERIODE")
    	private Integer periode; // correspond au mois en cours sur lequel on veut
    								// controler
    	@Column(name = "ZDATE")
    	@Temporal(TemporalType.DATE)
    	@JsonProperty("")
    	@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    	private Date date; // correspond à la date maximale au dela de laquelle on
    						// peut charger un fichier
    	@Column(name = "DEBLOQ")
    	private char debloque; // correspond à l'élément qui retire la condition de
    							// la limite de chargement
     
    	public Autorisation(@JsonProperty("periode") Integer periode,
    			@JsonProperty("date") Date date,
    			@JsonProperty("debloque") char debloque) {
    		this.date = date;
    		this.periode = periode;
    		this.debloque = debloque;
    	}
     
    	public Autorisation() {
    		periode = 0;
    		date = new Date();
    		debloque = 'x';
    	}
     
     
     
    	@JsonProperty("periode")
    	public Integer getPeriode() {
    		return periode;
    	}
     
    	public void setPeriode(Integer periode) {
    		this.periode = periode;
    	}
     
    	public Date getDate() {
    		return date;
    	}
     
    	public void setDate(Date date) {
    		this.date = date;
    	}
     
    	public char getDebloque() {
    		return debloque;
    	}
     
    	public void setDebloque(char debloque) {
    		this.debloque = debloque;
    	}
     
    //	@Override
    //	public int compareTo(Autorisation o) {
    //		// TODO Auto-generated method stub
    //		return 0;
    //	}
     
    	public boolean isAutorise() {
    		return debloque == 'X' ?true:false ;
    	}
     
    	private Date now() {
    		return new Date();
     
    	}
     
     
    }

    Le controller

    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
    package com.biocodex.elisa.resources;
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.List;
     
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
     
    import com.biocodex.elisa.domain.Autorisation;
    import com.biocodex.elisa.services.AutorisationService;
    import com.fasterxml.jackson.core.JsonGenerationException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
     
    @Controller
    @RequestMapping
    public class AutorisationController {
     
    	@Autowired
    	AutorisationService service;
     
    	public AutorisationController() {
    		init();
    	}
     
    	public List<Autorisation> findAll() {
     
    		return service.findAll();
    	}
     
    	public List<Autorisation> init() {
    		System.out.println("-------------*----------init: " + service);
    		if (service == null)
    			service = new AutorisationService();
    		System.out.println("service : " + service);
    		System.out.println("service 2 : " + service.findAll());
    		return service.findAll();
    	}
     
    	public static void main(String[] args) {
    		ObjectMapper mapper = new ObjectMapper();
    		AutorisationController controller = new AutorisationController();
    		List<Autorisation> autorisations;
    autorisations = controller.findAll();
    		Autorisation autorisation = new Autorisation();
     
     
    		String pathname = "C:\\projets\\Biocodex\\elisa\\fileTest.json";
     
    		try {
     
    			/*
    			 * écriture dans le fichier json
    			 */
    			mapper.writeValue(new File(pathname), autorisations);
    			System.out.println(pathname);
    		} catch (JsonGenerationException e) {
    			System.out
    					.println("---------------------ERREUR : JsonGenerationException"
    							+ e.getMessage());
    		} catch (JsonMappingException e) {
    			System.out
    					.println("---------------------ERREUR : JsonMappingException"
    							+ e.getMessage());
    		} catch (IOException e) {
    			System.out
    					.println("---------------------ERREUR : impossible de créer le fichier"
    							+ e.getMessage());
    		}
     
    		JSONParser parser = new JSONParser();
    		System.out.println("parser : " + parser);
    		try {
    			Object obj = parser.parse(new FileReader(pathname));
    			System.out.println("obj :" + obj);
    			JSONObject jsonObject = new JSONObject();
    			JSONArray array = (JSONArray) obj;
    			System.out.println("array : " + array);
    			Integer periode;
     
    			for (Object object : array) {
    				jsonObject = (JSONObject) object;
    				periode = (Integer) jsonObject.get("periode");
    				System.out.println("periode" + periode);
    			}
    		} catch (FileNotFoundException e) {
    			System.out
    					.println("---------------------ERREUR : Fichier introuvable "
    							+ e.getMessage());
    		} catch (IOException e) {
    			System.out
    					.println("---------------------ERREUR : Impossible de lire le fichier"
    							+ e.getMessage());
    		} catch (ParseException e) {
    			System.out.println("---------------------ERREUR : ParseException"
    					+ e.getMessage());
    		}
     
    	}
     
    }
    Le service

    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
    package com.biocodex.elisa.services;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.annotation.Resource;
     
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
     
    import com.biocodex.elisa.dao.AutorisationDAO;
    import com.biocodex.elisa.domain.Autorisation;
     
    @Service
    public class AutorisationService implements IAutorisationService{
    	private static final Logger LOGGER = LoggerFactory
    			.getLogger(AutorisationService.class);
     
    	@Resource
    	AutorisationDAO dao;
     
    	@Transactional (readOnly = true)
    	public List<Autorisation> findAll() {
    		List<Autorisation> autorisations = new ArrayList<Autorisation>();
    		System.out.println("autorisations : "+ dao.findAll());
    		for (Autorisation autorisation : dao.findAll()) {
     
    			autorisations.add(autorisation);
    		}
    		return autorisations;
    	}
     
    //	private Autorisation getByPeriode() {
    //		return dao.getByPeriode();
    //	}
     
    }

    La 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
    package com.biocodex.elisa.dao;
     
    import java.util.List;
    import java.util.Set;
     
    import javax.annotation.Resource;
     
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.query.Param;
     
    import com.biocodex.elisa.domain.Autorisation;
     
    @Resource
    public interface AutorisationDAO extends JpaRepository<Autorisation, Long> {
     
    	@Query (value = "Select a from Autorisation a")
    	@Override
    	public List<Autorisation> findAll();
     
     
     
     
    }
    La dto que je n'utilise pas vraiment pour l'instant parce que je ne sais pas trop à quoi elle peut servir.
    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
    package com.biocodex.elisa.dto;
     
    import java.io.Serializable;
     
    import com.biocodex.elisa.domain.Autorisation;
    import com.biocodex.elisa.resources.LCMResource.Status;
     
    public class AutorisationDTO implements Serializable {
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
    	private Autorisation autorisation;
    	private Status status;
    	private String workflowInst;
     
    	public AutorisationDTO() {
     
    	}
    //
    	public Autorisation getAutorisation() {
    		return autorisation;
    	}
    //
    	public void setAutorisation(Autorisation autorisation) {
    		this.autorisation = autorisation;
    	}
    //
    	public Status getStatus() {
    		return status;
    	}
    //
    	public void setStatus(Status status) {
    		this.status = status;
    	}
    //
    //	public String getWorkflowInst() {
    //		return workflowInst;
    //	}
    //
    //	public void setWorkflowInst(String workflowInst) {
    //		this.workflowInst = workflowInst;
    //	}
    	public String getWorkflowInst() {
    		return workflowInst;
    	}
    	public void setWorkflowInst(String workflowInst) {
    		this.workflowInst = workflowInst;
    	}
     
    }
    J'ai essayer de me baser sur l'architecture déjà existante du projet.

    Pour l'instant j'ai développé mon module en dehors du projet et mon module va récupérer mes infos en base et me créer un fichier JSON.

    Par contre ici, je n'arrive pas à récupérer les informations en base et j'ai même droit à un NullPointer sur le service alors que je l'instancie.
    Je ne sais pas si j'ai été très claire, j'ai la tête dans le guidon depuis des jours.
    Quelqu'un aurait une idée pour me venir en aide svp ?




    Ce code est la propriété de Biocodex et il ne doit être ni diffuser ni réutiliser.

  2. #2
    Expert confirmé Avatar de yildiz-online
    Homme Profil pro
    Architecte de domaine
    Inscrit en
    Octobre 2011
    Messages
    1 444
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Architecte de domaine

    Informations forums :
    Inscription : Octobre 2011
    Messages : 1 444
    Points : 4 563
    Points
    4 563
    Par défaut
    Avec Spring tu ne dois pas instancier manuellement tes services mais les déclarer en tant que beans
    PXL le retro-gaming facile: Essayez-le

    Yildiz-Engine an open-source modular game engine: Website
    Yildiz-Online a 3D MMORTS in alpha: Facebook page / Youtube page

  3. #3
    Futur Membre du Club
    Femme Profil pro
    Développeuse J2EE
    Inscrit en
    Mars 2015
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeuse J2EE
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2015
    Messages : 10
    Points : 7
    Points
    7
    Par défaut
    Citation Envoyé par yildiz-online Voir le message
    Avec Spring tu ne dois pas instancier manuellement tes services mais les déclarer en tant que beans
    Je te remercie pour ta réponse.

    Comment ça déclarer mon service en tant que bean ? Tu veux dire que tout ce que je mets dans mon bean à l'origine je dois le placer dans mon service ?

  4. #4
    Expert confirmé Avatar de yildiz-online
    Homme Profil pro
    Architecte de domaine
    Inscrit en
    Octobre 2011
    Messages
    1 444
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Architecte de domaine

    Informations forums :
    Inscription : Octobre 2011
    Messages : 1 444
    Points : 4 563
    Points
    4 563
    Par défaut
    Non mais tout ce que tu met dans ton bean doit être un bean pour que le cycle de vie de l'objet soit géré par spring.

    Là, il semble qu'il te manque l'implémentation du DAO, tu manipule une interface mais Spring n'a pas la concretisation de cette interface.
    PXL le retro-gaming facile: Essayez-le

    Yildiz-Engine an open-source modular game engine: Website
    Yildiz-Online a 3D MMORTS in alpha: Facebook page / Youtube page

  5. #5
    Futur Membre du Club
    Femme Profil pro
    Développeuse J2EE
    Inscrit en
    Mars 2015
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeuse J2EE
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2015
    Messages : 10
    Points : 7
    Points
    7
    Par défaut
    Citation Envoyé par yildiz-online Voir le message
    Non mais tout ce que tu met dans ton bean doit être un bean pour que le cycle de vie de l'objet soit géré par spring.

    Là, il semble qu'il te manque l'implémentation du DAO, tu manipule une interface mais Spring n'a pas la concretisation de cette interface.

    Très bien. Je vais essayer de mettre ça en place et je te donnes des nouvelles de l'avancée.
    Merci.

Discussions similaires

  1. [Framework] Spring Hibernate session java.sql.Connection config
    Par yLaplace dans le forum Spring
    Réponses: 0
    Dernier message: 22/01/2009, 10h16
  2. [Data] [Spring/Hibernate] sessionFactory null
    Par AyreoN dans le forum Spring
    Réponses: 3
    Dernier message: 12/11/2008, 17h48
  3. Novice:debuter avec java,Struts,Spring Hibernate
    Par makohsarah dans le forum Struts 1
    Réponses: 4
    Dernier message: 06/10/2007, 17h10

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