@OneToMany和@ManyToOne外键总是空的

时间:2015-06-16 00:24:55

标签: java mysql spring-mvc foreign-keys spring-data-jpa

这段代码有什么问题? 当我发布推文时,页面保持空白,当我检查数据库时,推文都在那里,但是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;
    }

}

0 个答案:

没有答案