Hibernate一对多添加Dao层中的save()方法不执行 20C

这是dao层中的方法

 // 增加设备
    @Override
    public void addDevice(Device device) {

        try {
            String hql = "from User u where u.userName=?";
            Query query = sessionFactory.getCurrentSession().createQuery(hql);
            query.setString(0, device.getOwnerName());
            User u = (User) query.uniqueResult();
            Device device1 = new Device();
            device1.setDeviceId(device.getDeviceId());
            device1.setDeviceName(device.getDeviceName());
            device1.setOwnerName(device.getOwnerName());
            device1.setDeviceOwner(u.getRealName());
            device1.setUser(u);
            sessionFactory.getCurrentSession().save(device1);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

这是设备实体类

 package com.plb.entities;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="T_DEVICE")
public class Device implements Serializable{

    /**
     * 设备实体类
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(length = 32)
    private String deviceId;//设备Id

    @Column(length = 32)
    private String deviceName;//设备名称

    @Column(length=32)
    private String deviceOwner;//设备所有者

    @Column(length = 32)
    private String ownerName;//设备所有着账号

    //Device对象的ownerName和User对象的userName字段相关联,

     @ManyToOne(cascade = CascadeType.ALL, optional = false)
     @JoinColumn(name="ownerName", referencedColumnName="userName",insertable=false,updatable=false)
     private User user;

    public String getDeviceId() {
        return deviceId;
    }
    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }
    public String getDeviceName() {
        return deviceName;
    }
    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getOwnerName() {
        return ownerName;
    }
    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }
    public String getDeviceOwner() {
        return deviceOwner;
    }
    public void setDeviceOwner(String deviceOwner) {
        this.deviceOwner = deviceOwner;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }


}

这是User实体类

 package com.plb.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "T_USER")
public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(length = 32)
    private String userId;

    @Column(length = 32)
    private String userName;

    @Column(length = 32)
    private String password;

    @Column(length = 32)
    private String realName;

    @Column(length = 32)
    private String sex;

    @Column(length = 32)
    private String age;

    @Column(length = 32)
    private String email;

    @Column(length = 32)
    private String tel;

    @Column(length = 32)
    private String remark;

    public String getUserId() {
        return userId;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

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

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

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

    public String getEmail() {
        return email;
    }

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

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

前台传给后台一个deviceId、deviceName、和用户账号userName,根据userName查询User表之后把deviceId、deviceName和查到的user都传给新new的Device对象deviceDebug1。后台所有的参数都能够获取,就是不执sessionFactory.getCurrentSession().save(device1);
也不报错也不抛异常,请求各位帮我看一下

1个回答

看看日志文件里面说了啥,会不会的配置文件有误?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐