2

我一直在寻找解决此问题的解决方案。我使用 Spring data JPA 从 MySQL 数据库服务器检索数据。

下面是实体:

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



}

服务等级:

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

}

最后我称之为..

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

这是我的 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 来创建我的内容。如果那是要看的东西..

4

1 回答 1

0

尝试将您的服务标记为事务性:

@Service
@Transactional
public class CustomerService {

只读方法标记为:

@Transactional(readOnly = true)
public Customer getTest() {
    return customerRepo.findOne(1);
}
于 2017-03-15T13:47:45.927 回答