在引用admin表hibernate的用户表上插入值1 [英] Insert a value of 1 on users table that is referencing admin table hibernate

查看:117
本文介绍了在引用admin表hibernate的用户表上插入值1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在用户表上保存值为1的用户表,该用户表在用户表上引用了具有adminId外键的管理表



在我的用户中。我有这个

  @OneToOne 
@JoinColumn(name =adminId)
Admin admin;

这是getter和setter用于上面映射到引用admin表的user.java



  public Admin getAdmins(){
return admin;
}

public void setAdmin(管理员ID){

this.admin = id;



$ b

在我的DAO类的SpringBoot应用程序中,我坚持像users表一样这个

  public void save(User user){
Admin admin =(Admin)getSession()。createQuery( from Admin a where a.adminId =?)
.setParameter(0,1);
user.setAdmin(admin);

getSession()。save(user);
}

EDITTED:
这是我如何保存用户

  public void save(User user){
long id = 1;

Admin admin =(Admin)getSession()。createQuery(from Admin a where a.adminId =?)。setParameter(0,1).list()。get(0);
user.setAdminz(admin);

getSession()。save(user);

$ / code>

这是我用于保存动作的控制器代码

  @RequestMapping(value =/ create-user,method = RequestMethod.POST)
public ModelAndView createUser(HttpServletRequest请求,
HttpServletResponse响应,
@RequestParam字符串名称,
@RequestParam字符串电子邮件){
尝试{
//创建新用户对象
User user = new User();
user.setName(name);
user.setEmail(email);
user.setTimestamp(new Date()。getTime());
$ b $ //将用户保存在数据库中(如果新增)
if(_userDao.getByEmail(email)== null){
_userDao.save(user);
}

当我尝试保存时出现此错误

  com.xxx.MainController [0; 39m [2m:[0; 39m创建用户的例外情况:

我已经修改映射到这个不接受空值

  @OneToOne 
@JoinColumn(name =adminId,nullable = false)
管理员管理员;

如果我删除nullable = false注释,则将NULL值保存到用户表的adminId字段中。



请问我做错了什么?请帮助。

解决方案

您必须执行您的查询

<$ p $ setParameter(0,1);}

Admin admin =(Admin)getSession()。createQuery(from Admin a where a.adminId =?)。

您的IDE应该警告您这一行,因为您不能将Hibernate Query转换为Admin类型,只要如果你没有将它实现到你的Admin类,如果你这样做,那是错的...



这个代码不是获取单个元素的最佳实践,但是你必须用list()方法执行你的查询。并从列表中获取您的元素。

  Admin admin =(Admin)getSession()。createQuery(from Admin a where a.adminId =?)。setParameter (0,1).list()。get(0)
//警告!如果列表为空,则会出现错误。

如果您想使用hibernate从数据库中选择单个元素(并使用实体映射)您可以使用此代码。

  Admin admin = getSession()。get(Admin.class,1); 


I am trying to save a value of 1 on users table that is referencing an admin table with a foreign key of adminId on the users table

In my User.java I have this

@OneToOne
    @JoinColumn(name = "adminId")
    Admin admin;

this is the getter and setter for the above mapping to the user.java referencing the admin table

public Admin getAdmins() {
        return admin;
    }

    public void setAdmin(Admin id) {

        this.admin = id;
    }

In my DAO class of SpringBoot application, I am persisting to the users table like this

public void save(User user) {
         Admin admin = (Admin)getSession().createQuery("from Admin a where a.adminId = ?")
                .setParameter(0, 1);
                user.setAdmin(admin);

        getSession().save(user);
    }

EDITTED: this is how I am trying to save a user

public void save(User user) {
      long id = 1 ;

        Admin admin = (Admin)getSession().createQuery("from Admin a where a.adminId = ?").setParameter(0, 1).list().get(0);
        user.setAdminz(admin);

        getSession().save(user);
    }

this is my controller code for saving action

@RequestMapping(value = "/create-user", method = RequestMethod.POST)
    public ModelAndView createUser(HttpServletRequest request,
                                   HttpServletResponse response,
                                   @RequestParam String name,
                                   @RequestParam String email) {
        try {
            // create new user object
            User user = new User();
            user.setName(name);
            user.setEmail(email);
            user.setTimestamp(new Date().getTime());

            // save user in db (if new)
            if (_userDao.getByEmail(email) == null) {
                _userDao.save(user);
            }

when I try to save I get this error

com.xxx.MainController       [0;39m [2m:[0;39m Exception in creating user:

I have modified my mapping to this not to accept nullable

@OneToOne
    @JoinColumn(name = "adminId",nullable = false)
    Admin admin;

If I remove nullable = false annotation, NULL value is saved into the adminId field of the user table.

Please what am I doing wrong? Kindly assist.

解决方案

You have to execute your query

Admin admin = (Admin)getSession().createQuery("from Admin a where a.adminId = ?").setParameter(0, 1);

Your IDE should warn you about this line because you cant cast Hibernate Query to your Admin type as long as if you didn't implement it to your Admin class, if you did that, that is so wrong...

This code is not best practice for getting single element but you have to execute your query with list() method. and get your element from list.

Admin admin = (Admin)getSession().createQuery("from Admin a where a.adminId = ?").setParameter(0, 1).list().get(0)
//Warning! If list is empty, there will be an error.

If you want to pick single element from db with using hibernate(and map with an entity) i recommend you to use this code.

Admin admin = getSession().get(Admin.class, 1); 

这篇关于在引用admin表hibernate的用户表上插入值1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆