Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @OneToMany和@ManyToOne外键始终为空_Java_Mysql_Spring Mvc_Foreign Keys_Spring Data Jpa - Fatal编程技术网

Java @OneToMany和@ManyToOne外键始终为空

Java @OneToMany和@ManyToOne外键始终为空,java,mysql,spring-mvc,foreign-keys,spring-data-jpa,Java,Mysql,Spring Mvc,Foreign Keys,Spring Data Jpa,这个代码有什么问题? 当我发布一条tweet时,页面保持空白,当我检查数据库时,tweet在那里,但userId键为null,这不是命名问题,我将其命名为user_id,但始终为null,我不知道问题是什么,这应该有效,不是吗 @Entity public class Tweets { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @Column(name =

这个代码有什么问题? 当我发布一条tweet时,页面保持空白,当我检查数据库时,tweet在那里,但userId键为null,这不是命名问题,我将其命名为user_id,但始终为null,我不知道问题是什么,这应该有效,不是吗

@Entity
public class Tweets {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;


    @Column(name = "tweet", length = 500)
    private String tweet;

    @ManyToOne
    @JoinColumn(name = "userId")
    private User user;


      public Tweets() {

        }

    public Tweets(String tweet, User user) {
        this.setTweet(tweet);
        this.setUser(user);
    }

    public long getId() {
        return id;
    }

    /*
    public void setId(long id) {
        this.id = id;
    }
    */

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }



    public String getTweet() {
        return tweet;
    }

    public void setTweet(String tweet) {
        this.tweet = tweet;
    } 

}
用户实体:

 @Entity
@Table(name = "usr", indexes = { @Index(columnList = "email", unique = true) })
// using usr because in may conflict with the name of the class
public class User {

    public static final int EMAIL_MAX = 250;
    public static final int NAME_MAX = 50;

    /*
     * public static enum Role {
     * 
     * UNVERIFIED, BLOCKED, ADMINISTRATOR
     * 
     * }
     */

    // primary key long, needs to be annotated with @Id
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    // add columns
    @Column(nullable = false, length = EMAIL_MAX)
    private String email;

    @Column(nullable = false, length = NAME_MAX)
    private String name;

    // no length, the password will be encrypted to some longer value than the
    // user enters
    @Column(nullable = false)
    private String password;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "user", cascade=CascadeType.ALL)
    private List<Tweets> tweets;


    public List<Tweets> getTweets() {
        return tweets;
    }

    public void setTweets(List<Tweets> tweets) {
        this.tweets = tweets;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(nullable = false)
    private String username;

    /*
     * //email verification code
     * 
     * @Column(length = 16) private String verificationCode;
     * 
     * public String getVerificationCode() { return verificationCode; }
     * 
     * public void setVerificationCode(String verificationCode) {
     * this.verificationCode = verificationCode; }
     * 
     * 
     * @ElementCollection(fetch = FetchType.EAGER) private Set<Role> roles = new
     * HashSet<Role>();
     * 
     * 
     * 
     * public Set<Role> getRoles() { return roles; }
     * 
     * public void setRoles(Set<Role> roles) { this.roles = roles; }
     */

    public long getId() {
        return id;
    }
/*
    public void setId(long id) {
        this.id = id;
    }

    */

    public String getEmail() {
        return email;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public boolean isEditable() {
        User loggedIn = MyTools.getSessionUser();

        if (loggedIn == null) {
            return false;
        }

        return loggedIn.getId() == id;
    }

    public String getUsername() {

        return username;
    }

}
@实体
@表(name=“usr”,Index={@Index(columnList=“email”,unique=true)})
//使用usr,因为in可能与类的名称冲突
公共类用户{
公共静态最终int电子邮件_MAX=250;
公共静态最终整数名_MAX=50;
/*
*公共静态枚举角色{
* 
*未验证、阻止、管理员
* 
* }
*/
//主键长,需要用@Id注释
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
//添加列
@列(nullable=false,length=EMAIL_MAX)
私人字符串电子邮件;
@列(nullable=false,length=NAME\u MAX)
私有字符串名称;
//无长度,密码将被加密为比
//用户输入
@列(nullable=false)
私有字符串密码;
@OneToMany(fetch=FetchType.EAGER,mappedBy=“user”,cascade=CascadeType.ALL)
私人列表推文;
公共列表getTweets(){
返回推文;
}
公共无效设置tweets(列出tweets){
this.tweets=tweets;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
@列(nullable=false)
私有字符串用户名;
/*
*//电子邮件验证码
* 
*@Column(长度=16)私有字符串验证代码;
* 
*公共字符串getVerificationCode(){return verificationCode;}
* 
*public void setVerificationCode(字符串验证代码){
*this.verificationCode=verificationCode;}
* 
* 
*@ElementCollection(fetch=FetchType.EAGER)私有集角色=新建
*HashSet();
* 
* 
* 
*public Set getRoles(){return roles;}
* 
*public void setRoles(Set roles){this.roles=roles;}
*/
公共长getId(){
返回id;
}
/*
公共无效集合id(长id){
this.id=id;
}
*/
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共布尔值可编辑(){
用户loggedIn=MyTools.getSessionUser();
if(loggedIn==null){
返回false;
}
返回loggedIn.getId()==id;
}
公共字符串getUsername(){
返回用户名;
}
}

在持久化Tweets实例之前,您是否在调用setUser?是的,我就是这样解决的