2017-03-15 11 views
2

私はこの問題を解決するための解決策を探していました。私は、SpringデータJPAを使用して、MySQLデータベースサーバからデータを取得します。SpringのJPAリポジトリfindOne()はnullを返しますか?

以下

エンティティです:

/* 
* 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 + " ]"; 
    } 

} 

リポジトリクラス:

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>{ 



} 

サービスクラス:私は

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()); 
} 
...それを呼び出す

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); 
    } 

} 

、最終的に

これは私の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 

私のプログラムはそれを呼び出すしかしfindOneメソッドの戻り値はnullです。実際にはデータベースにプライマリキー1を持つ顧客レコードがあるため、そうではありません。私はあなたが問題を見つけ出すのに必要なものをすべて含んでいることを願っています。私はVaadinを使って自分のコンテンツを作成します。それは見て何かあれば...トランザクションとしてあなたのサービスをマーキング

+0

それはあなたがに接続しているデータベースが異なる可能ですお客様のレコードが存在することを確認するために表示されているものからですか?私も同様の問題がありましたが、間違ったDBを見ていることに気付きました。 – heez

答えて

0

試してみてください。

@Service 
@Transactional 
public class CustomerService { 

読み取り専用のメソッドマークとして:

@Transactional(readOnly = true) 
public Customer getTest() { 
    return customerRepo.findOne(1); 
} 
関連する問題