为什么我的弹簧实体之一需要5秒钟以上的时间进行查询

时间:2019-03-01 00:16:57

标签: java mysql spring

我有一个Spring后端应用程序,该应用程序具有30多个实体,但是由于某种原因,该用户对象永远需要做任何事情。仅检索所有用户(仅15位)就需要6秒钟!!除此之外,只需调用

User user = new User();

超过100毫秒,这对我来说毫无意义。我不知道是什么引起了这个问题。下面是用户模型和通过查询所有用户生成的一些sql。

@Entity
@Table(name = "tbluser")
@Immutable
public class User {

    private int userId;
    private String password;
    private String company;
    private String firstName;
    private String lastName;
    private String email;
    private String userName;
    private String phone;
    private String address1;
    private String address2;
    private String country;
    private String postal;
    private Set<UserRole> roles;
    private Integer isActive;
    private String userFolder;

    public User(){
        this("new", "PASSWORD", "new", "new", "",  1, "", "", "", "", "", "");
    }

    public User(String userName, String password, String firstName, String lastName){
        this(userName, password, firstName, lastName, "", 1, "", "", "", "", "", "");
    }

    public User(String userName, String password, String firstName, String lastName, Integer isActive){
        this(userName, password, firstName, lastName, "",  isActive, "", "", "", "", "", "");
    }

    public User(String userName, String password, String firstName, String lastName, String email, Integer isActive,
         String company, String phone, String address1, String address2, String country, String postal
         ){
        this.setUserName(userName);
        this.setEmail(email);
        this.setPassword(new BCryptPasswordEncoder().encode(password));
        this.setFirstName(firstName);
        this.setLastName(lastName);
        this.setIsActive(isActive);
        this.setCompany(company);
        this.setPhone(phone);
        this.setAddress1(address1);
        this.setAddress2(address2);
        this.setCountry(country);
        this.setPostal(postal);
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Integer getUserId() {
        return userId;
    }
    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @Column(name = "username")
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Column(name = "password", updatable = false)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "company")
    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    @Column(name = "firstname")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "lastname")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name = "email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "phone")
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Column(name = "address1")
    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    @Column(name = "address2")
    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    @Column(name = "country")
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Column(name = "postal")
    public String getPostal() {
        return postal;
    }

    public void setPostal(String postal) {
        this.postal = postal;
    }

    @Column(name = "user_folder")
    public String getUserFolder() {
        return userFolder;
    }

    public void setUserFolder(String userFolder) {
        this.userFolder = userFolder;
    }

    @Column(name = "isactive")
    public Integer getIsActive() {
        return isActive;
    }

    public void setIsActive(Integer isActive) {
        this.isActive = isActive;
    }


    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "tbluserrolemap", joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id"))
    public Set<UserRole> getRoles() {
        return roles;
    }

    public void setRoles(Set<UserRole> roles) {
        this.roles = roles;
    }



}

这是生成的sql以及用户角色的一些联接。应该热切地加载它,但是即使用户数量也不多,也应该不会有太大的改变。

Hibernate: 
    select
        user0_.userId as userId1_63_,
        user0_.address1 as address2_63_,
        user0_.address2 as address3_63_,
        user0_.company as company4_63_,
        user0_.country as country5_63_,
        user0_.email as email6_63_,
        user0_.firstname as firstnam7_63_,
        user0_.isActive as isActive8_63_,
        user0_.lastname as lastname9_63_,
        user0_.password as passwor10_63_,
        user0_.phone as phone11_63_,
        user0_.postal as postal12_63_,
        user0_.user_folder as user_fo13_63_,
        user0_.username as usernam14_63_ 
    from
        tbluser user0_
Hibernate: 
select
    roles0_.user_id as user_id1_64_0_,
    roles0_.role_id as role_id2_64_0_,
    userrole1_.role_id as role_id1_3_1_,
    userrole1_.name as name2_3_1_ 
from
    tbluserrolemap roles0_ 
inner join
    tbluserroles userrole1_ 
        on roles0_.role_id=userrole1_.role_id 
where
    roles0_.user_id=?
Hibernate: 
select
    roles0_.user_id as user_id1_64_0_,
    roles0_.role_id as role_id2_64_0_,
    userrole1_.role_id as role_id1_3_1_,
    userrole1_.name as name2_3_1_ 
from
    tbluserrolemap roles0_ 
inner join
    tbluserroles userrole1_ 
        on roles0_.role_id=userrole1_.role_id 
where
    roles0_.user_id=?
Hibernate: 
select
    roles0_.user_id as user_id1_64_0_,
    roles0_.role_id as role_id2_64_0_,
    userrole1_.role_id as role_id1_3_1_,
    userrole1_.name as name2_3_1_ 
from
    tbluserrolemap roles0_ 
inner join
    tbluserroles userrole1_ 
        on roles0_.role_id=userrole1_.role_id 
where
    roles0_.user_id=?

CREATE TABLE `tbluser` (
  `userId` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(45) NOT NULL,
  `lastname` varchar(45) NOT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `username` varchar(45) DEFAULT NULL,
  `phone` varchar(45) DEFAULT NULL,
  `address1` varchar(45) DEFAULT NULL,
  `address2` varchar(45) DEFAULT NULL,
  `country` varchar(45) DEFAULT NULL,
  `postal` varchar(45) DEFAULT NULL,
  `isactive` int(8) DEFAULT NULL,
  `company` varchar(45) DEFAULT NULL,
  `user_folder` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`userId`)
)

CREATE TABLE `tbluserrolemap` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `role_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_roleid_idx` (`role_id`),
  KEY `fk_userid_idx` (`user_id`),
  CONSTRAINT `fk_roleid` FOREIGN KEY (`role_id`) REFERENCES `tbluserroles` (`role_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_userid` FOREIGN KEY (`user_id`) REFERENCES `tbluser` (`userId`) ON DELETE NO ACTION ON UPDATE NO ACTION
)

CREATE TABLE `tbluserroles` (
  `role_id` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`role_id`)
)

1 个答案:

答案 0 :(得分:-1)

原来是我要覆盖用户实体的空构造函数。显然,Spring确实不喜欢这样。删除了该内容,并在10毫秒内执行了调用。