微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Java项目中使用Spring和SQL连接三个表

如何解决如何在Java项目中使用Spring和SQL连接三个表

我正在做一个有三个表的项目。 (A-用户,B-信息,C-计算)。我已经在每个表中填充了“虚拟值”,以便可以测试该应用程序。

表B 表A 连接,表C与表B 连接。 像这样的 A-

MysqL中设置表可以正常工作,但是当我尝试在Intellij中将其与spring连接时,会收到消息->

Fri Oct 09 16:55:00 CEST 2020
There was an unexpected error (type=Internal Server Error,status=500).
Could not prepare statement; sql [select user0_.id as id1_2_,user0_.email as email2_2_,user0_.first_name as first_na3_2_,user0_.last_name as last_nam4_2_,user0_.password as password5_2_,user0_1_.age as age1_1_,user0_1_.exercise_level as exercise2_1_,user0_1_.height as height3_1_,user0_1_.user_gender as user_gen4_1_,user0_1_.weight as weight5_1_,user0_2_.bmi as bmi1_0_,user0_2_.bmr as bmr2_0_,user0_2_.prebmr as prebmr3_0_,case when user0_2_.id is not null then 2 when user0_1_.id is not null then 1 when user0_.id is not null then 0 end as clazz_ from user user0_ left outer join info user0_1_ on user0_.id=user0_1_.id left outer join calculations user0_2_ on user0_.id=user0_2_.id where user0_.email=?]; nested exception is org.hibernate.exception.sqlGrammarException: Could not prepare statement 

当我尝试使用“虚拟用户”之一登录时,上面的消息出现了,因此该服务似乎很难从其他表中获取所有数据?

用户类别:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column(name = "first_name")
    @Size(min = 3,max = 20)
    private String firstName;
    @Column(name = "last_name")
    @Size(min = 3,max = 30)
    private String lastName;
    @Size(min = 6,max = 50)
    private String password;
    @Column(name = "email")
    @Email
    private String email;

    public User(){}

    public User(String firstName,String lastName,String password,String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

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

    public String getLastName() {
        return lastName;
    }

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

    public String getpassword() {
        return password;
    }

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

    public String getEmail() {
        return email;
    }

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

}

用户存储库:

@Repository
public interface UserRepository extends CrudRepository<User,Integer> {

    User findByEmail (String email);

}

信息类:

@Entity
@PrimaryKeyJoinColumn
@Inheritance(strategy = InheritanceType.JOINED)
public class Info extends User {

    @Id
    private int id;
    @Enumerated(EnumType.STRING)
    @Column(name = "user_gender")
    private UserGender userGender;
    private int age;
    private float height;
    private float weight;
    @Enumerated(EnumType.STRING)
    @Column(name = "exercise_level")
    private ExerciseLevel exerciseLevel;
//    @Column(name = "user_id")
//    private Integer userId;

    public Info() {
    }

    public Info(String firstName,String email,UserGender userGender,int age,float height,float weight,ExerciseLevel exerciseLevel) {
        super(firstName,lastName,password,email);
        this.userGender = userGender;
        this.age = age;
        this.height = height;
        this.weight = weight;
        this.exerciseLevel = exerciseLevel;
//        this.userId = userId;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    public UserGender getUserGender() {
        return userGender;
    }

    public void setUserGender(UserGender userGender) {
        this.userGender = userGender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

    public ExerciseLevel getExerciseLevel() {
        return exerciseLevel;
    }

    public void setExerciseLevel(ExerciseLevel  exerciseLevel) {
        this.exerciseLevel = exerciseLevel;
    }

//    public Integer getUserId() {
//        return userId;
//    }
//
//    public void setUserId(Integer userId) {
//    this.userId = userId;
//    }

}

信息存储库:

@Repository
public interface InfoRepository extends CrudRepository<Info,Integer> {
    
}

计算类别:

@Entity
@PrimaryKeyJoinColumn
public class Calculations extends Info {

    @Id
    private int id;
    private float bmi;
    private float bmr;
    float preBMR;
//    @Column(name = "info_id")
//    private Integer infoId;

    public void CalculateBMI() {
        bmi = (100 * 100 * getWeight() / (getHeight() * getHeight()));
        setBmi(bmi);
    }

    public void CalculateBMR() {

        if (getUserGender().equals(UserGender.MALE)) {
            preBMR = (66.47f + (getWeight() * 13.7f) + (getHeight() * 5f) - (getAge() * 6.8f));
        }
        if (getUserGender().equals(UserGender.FEMALE)) {
            preBMR = (655 + (getWeight() * 9.6f) + (getHeight() * 1.8f) - (getAge() * 4.7f));
        }

        switch (getExerciseLevel()) {
            case SEDENTARY:
                bmr = (preBMR * 1.2f);
                break;
            case LIGHTLY_ACTIVE:
                bmr = (preBMR * 1.375f);
                break;
            case MODERATELY_ACTIVE:
                bmr = (preBMR * 1.5f);
                break;
            case VERY_ACTIVE:
                bmr = (preBMR * 1.725f);
                break;
            case SUPER_ACTIVE:
                bmr = (preBMR * 1.9f);
                break;

        } setBmr(bmr);

    }

    public Calculations() {
    }


    public Calculations(String firstName,ExerciseLevel exerciseLevel,float bmi,float bmr) {
        super(firstName,email,userGender,age,height,weight,exerciseLevel);
        this.bmi = bmi;
        this.bmr = bmr;
//        this.infoId = infoId;

    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    public float getBmi() {
        return bmi;
    }

    public void setBmi(float bmi) {
        this.bmi = bmi;
    }

    public float getBmr() {
        return bmr;
    }

    public void setBmr(float bmr) {
        this.bmr = bmr;
    }

//    public Integer getInfoId() {
//        return infoId;
//    }
//
//    public void setInfoId(Integer infoId) {
//        this.infoId = infoId;
//    }

}

计算存储库:

@Repository
public interface CalculationsRepository extends CrudRepository<Info,Integer> {
}

架构文件

CREATE TABLE user
(
    id         BIGINT AUTO_INCREMENT PRIMARY KEY,first_name VARCHAR(20),last_name  VARCHAR(30),password   VARCHAR(50),email      VARCHAR(50)
);

CREATE TABLE info
(
    id             BIGINT,user_gender    ENUM ('MALE','FEMALE'),age            INT(2),height         INT(3),weight         INT(3),exercise_level ENUM ('SEDENTARY','LIGHTLY_ACTIVE','MODERATELY_ACTIVE','VERY_ACTIVE','SUPER_ACTIVE'),user_id        BIGINT,FOREIGN KEY (user_id) REFERENCES user(id)
);

CREATE table calculations
(
    id  BIGINT,bmi FLOAT(50),bmr FLOAT(50),info_id BIGINT,FOREIGN KEY (info_id) REFERENCES info(id)
);

数据输入:

INSERT INTO user (first_name,last_name,email)
VALUES ('Abdi','Mohamed','123456','Abdi123@gmail.com'),('Ola','nordmann','Ola123@gmail.com'),('Kari','Madsen','Kari123@gmail.com'),('Christer','Falck','Christer123@gmail.com'),('Sonja','Agnes','Sonja123@gmail.com'),('Kristine','Jensen','Kristine123@gmail.com');

INSERT INTO info (id,user_gender,exercise_level,user_id)
VALUES (1,'MALE',18,184,78,'SUPER_ACTIVE',1),(2,38,160,100,'SEDENTARY',2),(3,'FEMALE',22,152,48,3),(4,51,189,98,4),(5,34,159,61,5),(6,44,164,62,6);

INSERT INTO calculations(id,bmi,bmr,info_id)
VALUES (1,21,6);

我不知道该如何设置,Enitity或外键/主键有问题吗?我应该在表中添加更多内容吗?我的设置是执行此操作的最佳方法吗?

我真的很感谢在此方面的一些帮助,这对编程界来说是很新的,这是我第一次独自完成一个项目。

让我知道你们是否需要一些我的项目的片段,以帮助解决问题

提前感谢:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。