如何避免使用jpa在db中的外键值中获取空值?

时间:2014-05-28 07:57:06

标签: java mysql hibernate jsp jpa

我使用jpa在db.iam中获取foregin键值null。 我有两个表,一个是用户另一个是review.i想在审查table.review表中存储用户id值iam得到null为user_id column.plz帮助我如何存储用户id值... 感谢

这是我用户的api课程

public interface User
{
    public int getId();
    public void setId(int id);         
    public String getFirstName();
    public void setFirstName(String firstName);        
     public String getLastName();
    public void setLastName(String lastName);  

} 

这是我的评论api课程

public interface Review
{
    public int getId();
    public void setId(int id);         
    public String getReview();
    public void setReview(String review);    
    public User getUserId();
    public void setUserId(User userId);

}

这是我的评审模型类

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

public int getId()
{
    return id;
}

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

@Basic(optional = true)
@Column(name = "reviews")
private String review;

public String getReview()
{
    return review;
}

public void setReview(String review) 
{
    this.review = review;
}   
 @OneToOne(targetEntity = com.movi.db.model.UserImpl.class, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private User userId;

public User getUserId()
{
    return userId;
}

public void setUserId(User userId) 
{
    this.userId = userId;
}    

这是我的道法

public boolean createReview(Review review)
{
    em.getTransaction().begin();    
    em.persist(review);         
    em.getTransaction().commit();
    return true;
}

这是我的动作类

    private ReviewImpl review;
    private String userReview;
    private String reply;
    private String send;
    private User user;
      //setters and getters
  if(reply!=null)
        {
            review.setUserId(user);                  
            if(service.createReview(review))
                System.out.println("created review successful");
            else
                System.out.println("creation failed");
            return "userreview";
        }   

0 个答案:

没有答案