2017-03-15 1 views
2

J'ai cherché une solution pour résoudre ce problème. J'utilise Spring data JPA pour extraire des données d'un serveur de base de données MySQL.Référentiel JPA de printemps findOne() return null?

Ci-dessous est l'entité:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.parko.timebestilling.database.entity; 

import java.io.Serializable; 
import java.util.Collection; 
import javax.persistence.Basic; 
import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.NamedQueries; 
import javax.persistence.NamedQuery; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlTransient; 

/** 
* 
* @author Christian 
*/ 
@Entity 
@Table(name = "customer") 
@XmlRootElement 
@NamedQueries({ 
     @NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"), 
     @NamedQuery(name = "Customer.findByCID", query = "SELECT c FROM Customer c WHERE c.cID = :cID"), 
     @NamedQuery(name = "Customer.findByCAdress", query = "SELECT c FROM Customer c WHERE c.cAdress = :cAdress"), 
     @NamedQuery(name = "Customer.findByCEpost", query = "SELECT c FROM Customer c WHERE c.cEpost = :cEpost"), 
     @NamedQuery(name = "Customer.findByCLastName", query = "SELECT c FROM Customer c WHERE c.cLastName = :cLastName"), 
     @NamedQuery(name = "Customer.findByCName", query = "SELECT c FROM Customer c WHERE c.cName = :cName"), 
     @NamedQuery(name = "Customer.findByCNote", query = "SELECT c FROM Customer c WHERE c.cNote = :cNote"), 
     @NamedQuery(name = "Customer.findByCPhonenumber", query = "SELECT c FROM Customer c WHERE c.cPhonenumber = :cPhonenumber"), 
}) 
public class Customer implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "cID") 
    private Integer cID; 
    @Size(max = 255) 
    @Column(name = "cAdress") 
    private String cAdress; 
    @Size(max = 255) 
    @Column(name = "cEpost") 
    private String cEpost; 
    @Size(max = 255) 
    @Column(name = "cLastName") 
    private String cLastName; 
    @Size(max = 255) 
    @Column(name = "cName") 
    private String cName; 
    @Size(max = 255) 
    @Column(name = "cNote") 
    private String cNote; 
    @Size(max = 255) 
    @Column(name = "cPhonenumber") 
    private String cPhonenumber; 
    /* 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "cid") 
    private Collection<SmsHistory> smsHistoryCollection; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer") 
    private Collection<SmsReceivers> smsReceiversCollection; 
    @OneToMany(mappedBy = "cid") 
    private Collection<Reservation> reservationCollection; 
    */ 
    public Customer() { 
    } 

    public Customer(Integer cID) { 
     this.cID = cID; 
    } 

    public Customer(Integer cID, String cAdress1, String cEpost1, String cLastName1, String cName1, String cPhonenumber1) { 
     this.cID = cID; 
     this.cAdress = cAdress1; 
     this.cEpost = cEpost1; 
     this.cLastName = cLastName1; 
     this.cName = cName1; 
     this.cPhonenumber = cPhonenumber1; 
    } 

    public Integer getCID() { 
     return cID; 
    } 

    public void setCID(Integer cID) { 
     this.cID = cID; 
    } 

    public String getCAdress() { 
     return cAdress; 
    } 

    public void setCAdress(String cAdress) { 
     this.cAdress = cAdress; 
    } 

    public String getCEpost() { 
     return cEpost; 
    } 

    public void setCEpost(String cEpost) { 
     this.cEpost = cEpost; 
    } 

    public String getCLastName() { 
     return cLastName; 
    } 

    public void setCLastName(String cLastName) { 
     this.cLastName = cLastName; 
    } 

    public String getCName() { 
     return cName; 
    } 

    public void setCName(String cName) { 
     this.cName = cName; 
    } 

    public String getCNote() { 
     return cNote; 
    } 

    public void setCNote(String cNote) { 
     this.cNote = cNote; 
    } 

    public String getCPhonenumber() { 
     return cPhonenumber; 
    } 

    public void setCPhonenumber(String cPhonenumber) { 
     this.cPhonenumber = cPhonenumber; 
    } 

    /* 

    @XmlTransient 
    public Collection<SmsHistory> getSmsHistoryCollection() { 
     return smsHistoryCollection; 
    } 

    public void setSmsHistoryCollection(Collection<SmsHistory> smsHistoryCollection) { 
     this.smsHistoryCollection = smsHistoryCollection; 
    } 

    @XmlTransient 
    public Collection<SmsReceivers> getSmsReceiversCollection() { 
     return smsReceiversCollection; 
    } 

    public void setSmsReceiversCollection(Collection<SmsReceivers> smsReceiversCollection) { 
     this.smsReceiversCollection = smsReceiversCollection; 
    } 

    @XmlTransient 
    public Collection<Reservation> getReservationCollection() { 
     return reservationCollection; 
    } 

    public void setReservationCollection(Collection<Reservation> reservationCollection) { 
     this.reservationCollection = reservationCollection; 
    } 
    */ 
    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (cID != null ? cID.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Customer)) { 
      return false; 
     } 
     Customer other = (Customer) object; 
     if ((this.cID == null && other.cID != null) || (this.cID != null && !this.cID.equals(other.cID))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "com.parko.timebestilling.database.entity.Customer[ cID=" + cID + " ]"; 
    } 

} 

classe Repository:

package com.parko.timebestilling.database.beans; 

import com.parko.timebestilling.database.entity.Customer; 
import org.springframework.data.repository.CrudRepository; 

/** 
* Created by christian on 15.03.2017. 
*/ 

public interface CustomerRepository extends CrudRepository<Customer, Integer>{ 



} 

Classe de service:

package com.parko.timebestilling.database.beans; 

import com.parko.timebestilling.database.entity.Customer; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by christian on 15.03.2017. 
*/ 
@Service 
public class CustomerService { 

    @Autowired 
    private CustomerRepository customerRepo; 

    public List<Customer> getAllCustomers() { 
     List<Customer> customers = new ArrayList<>(); 
     customerRepo.findAll().forEach(customers::add); 
     return customers; 
    } 

    public Customer getTest() { 
     return customerRepo.findOne(1); 
    } 

} 

et enfin où je l'appelle ..

public class Welcome extends CssLayout implements View { 

    public static final String VIEW_NAME = "Hjem"; 

    @Autowired 
    CustomerService customerServ; 

    public Welcome() { 
     System.out.println(customerServ.getTest().getcName()); 
} 

Ceci est mon application.properties

# =============================== 
# = DATA SOURCE 
# =============================== 

# Set here configurations for the database connection 

# Connection url for the database "Parko" 
spring.datasource.url = jdbc:mysql://localhost:3306/parko?useSSL=false 

# Username and password 
spring.datasource.username = root 
spring.datasource.password = root 

# Keep the connection alive if idle for a long time (needed in production) 
spring.datasource.testWhileIdle = true 
spring.datasource.validationQuery = SELECT 1 

# =============================== 
# = JPA/HIBERNATE 
# =============================== 

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is 
# stripped before adding them to the entity manager). 

# Show or not log for each sql query 
spring.jpa.show-sql = true 

# Hibernate ddl auto (create, create-drop, update): with "update" the database 
# schema will be automatically updated accordingly to java entities found in 
# the project 
spring.jpa.hibernate.ddl-auto = update 

# Naming strategy 
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy 

spring.data.jpa.repositories.enabled=true 

# Allows Hibernate to generate SQL optimized for a particular DBMS 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 

spring.jpa.database=mysql 

# =============================== 
# = SESSION 
# =============================== 

spring.session.store-type=none 


# =============================== 
# = VAADIN 
# =============================== 
vaadin.servlet.productionMode=true 

Cependant, le return null méthode findOne quand appeler mon programme il. Il y a en effet un enregistrement client avec la clé primaire 1 dans la base de données, ce n'est donc pas le cas. J'espère avoir inclus tout ce dont vous avez besoin pour repérer le problème. J'utilise Vaadin pour créer mon contenu. si c'est quelque chose à regarder ..

+0

Est-il possible de la base de données que vous connectez est différent de celui que vous consultez pour confirmer que le dossier client existe? J'ai eu un problème similaire, mais j'ai réalisé que je regardais la mauvaise DB. – heez

Répondre

0

Essayez de sélectionner votre service comme transactionnel:

@Service 
@Transactional 
public class CustomerService { 

lecture seule des méthodes de marquage comme:

@Transactional(readOnly = true) 
public Customer getTest() { 
    return customerRepo.findOne(1); 
}