Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 @ManyToOne关系的主键未插入_Java_Hibernate_Spring Mvc_Jpa - Fatal编程技术网

Java @ManyToOne关系的主键未插入

Java @ManyToOne关系的主键未插入,java,hibernate,spring-mvc,jpa,Java,Hibernate,Spring Mvc,Jpa,我有一个应用程序(Spring4MVC+Hibernate4+MySQL+Maven集成示例使用注释),使用基于注释的配置将Spring与Hibernate集成。 我有这个域对象: @Entity @Table(name="t_device") public class Device { enum Type { IOS, ANDROID } public Device() { super(); }

我有一个应用程序(Spring4MVC+Hibernate4+MySQL+Maven集成示例使用注释),使用基于注释的配置将Spring与Hibernate集成。 我有这个域对象:

@Entity
@Table(name="t_device")
public class Device {

    enum Type {
        IOS, 
        ANDROID
    }



    public Device() {
        super();
    }

    public Device(String key) {
        super();
        this.key = key;
    }

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

    @NotEmpty
    @Size(min=1, max=50)
    @Column(name = "device_key", unique=true, nullable = false)
    private String key;


    @Column(name = "device_desc")
    private String desc;

    @Enumerated(EnumType.STRING)
    @Column(name = "device_type")
    private Type type;


    @OneToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "application_id", 
                referencedColumnName = "id")
    private Application application;


    public int getId() {
        return id;
    }

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

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Application getApplication() {
        return application;
    }

    public void setApplication(Application application) {
        this.application = application;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((desc == null) ? 0 : desc.hashCode());
        result = prime * result + id;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Device other = (Device) obj;
        if (desc == null) {
            if (other.desc != null)
                return false;
        } else if (!desc.equals(other.desc))
            return false;
        if (id != other.id)
            return false;
        if (key == null) {
            if (other.key != null)
                return false;
        } else if (!key.equals(other.key))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Device [id=" + id + ", key=" + key + ", desc=" + desc + "]";
    }
}
链接到另一个的:

@Entity
@Table(name="t_device_event")
public class DeviceEvent {

    public class Coordinates {

        @Column(name = "device_lat")
        private Double lat;

        @Column(name = "device_lng")
        private Double lng;


        public Coordinates(Double lat, Double lng) {
            super();
            this.lat = lat;
            this.lng = lng;
        }

        public Double getLat() {
            return lat;
        }

        public void setLat(Double lat) {
            this.lat = lat;
        }

        public Double getLng() {
            return lng;
        }

        public void setLng(Double lng) {
            this.lng = lng;
        }

    }

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

    @ManyToOne
    private Device device;

    private Long received;

    private String message;

    @Transient
    private Coordinates coordinates;


    public Coordinates getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(Coordinates coordinates) {
        this.coordinates = coordinates;
    }

    public int getId() {
        return id;
    }

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

    public Device getDevice() {
        return device;
    }

    public void setDevice(Device device) {
        this.device = device;
    }

    public Long getReceived() {
        return received;
    }

    public void setReceived(Long received) {
        this.received = received;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public DeviceEvent(Device device) {
        super();
        this.device = device;
    }   
}
控制器中的这段代码:

  Device device = deviceService.findByKey("AS3989E506");

    DeviceEvent deviceEvent = new DeviceEvent(device);
    deviceEvent.setCoordinates(deviceEvent.new Coordinates(Double.MIN_VALUE, Double.MAX_VALUE));
    deviceEvent.setMessage("message");
    deviceEvent.setReceived(new Date().getTime());

    deviceEventService.save(deviceEvent);   
和hibernate控制台:

休眠:

  select
        this_.id as id1_1_1_,
        this_.application_id as applicat5_1_1_,
        this_.device_desc as device_d2_1_1_,
        this_.device_key as device_k3_1_1_,
        this_.device_type as device_t4_1_1_,
        applicatio2_.id as id1_0_0_,
        applicatio2_.application_desc as applicat2_0_0_,
        applicatio2_.application_key as applicat3_0_0_ 
    from
        t_device this_ 
    left outer join
        t_application applicatio2_ 
            on this_.application_id=applicatio2_.id 
    where
        this_.device_key=?
Hibernate: 
    insert 
    into
        t_device_event
        (device_id, message, received) 
    values
        (?, ?, ?)
Hibernate: 
    select
        this_.id as id1_1_1_,
        this_.application_id as applicat5_1_1_,
        this_.device_desc as device_d2_1_1_,
        this_.device_key as device_k3_1_1_,
        this_.device_type as device_t4_1_1_,
        applicatio2_.id as id1_0_0_,
        applicatio2_.application_desc as applicat2_0_0_,
        applicatio2_.application_key as applicat3_0_0_ 
    from
        t_device this_ 
    left outer join
        t_application applicatio2_ 
            on this_.application_id=applicatio2_.id
以下是服务:

@Service("deviceEventService")
@Transactional
public class DeviceEventServiceImpl implements DeviceEventService {

    @Autowired
    private DeviceEventDao dao;


    public void save(DeviceEvent deviceEvent) {
        dao.save(deviceEvent);      
    }

}
另一个:

@Service("deviceService")
@Transactional
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceDao dao;

    public Device findById(int id) {
        return dao.findById(id);
    }


    public void save(Device device) {
        dao.save(device);
    }


    public void update(Device device) {
        // TODO Auto-generated method stub

    }


    public void delete(Device device) {
        // TODO Auto-generated method stub
    }


    public List<Device> findAll() {
        return dao.findAll();
    }


    public Device findByKey(String key) {
        return dao.findByKey(key);
    }


    public boolean isDeviceKeyUnique(Integer id, String key) {
        Device device = findByKey(key);
        return ( device == null || ((id != null) && (device.getId() == id)));
    }


    public void deleteByKey(String key) {
        dao.deleteByKey (key);  
    }



}
@服务(“设备服务”)
@交易的
公共类DeviceServiceImpl实现DeviceService{
@自动连线
私人设备道道;
公共设备findById(int-id){
返回dao.findById(id);
}
公共无效保存(设备){
保存(设备);
}
公共无效更新(设备){
//TODO自动生成的方法存根
}
公共无效删除(设备){
//TODO自动生成的方法存根
}
公共列表findAll(){
返回dao.findAll();
}
公共设备findByKey(字符串键){
返回dao.findByKey(键);
}
公共布尔值isDeviceKeyUnique(整数id,字符串键){
设备设备=findByKey(键);
返回(device==null | |((id!=null)&&&(device.getId()==id));
}
公共void deleteByKey(字符串键){
dao.deleteByKey(key);
}
}

但是表t\u device\u event的字段device\u id为空

可能是
@JoinColumn
注释将帮助您

@ManyToOne
@JoinColumn(name="device_id")
private Device device;

在创建设备事件的事务关闭后,您确定正在执行查询以检查设备id吗?