hibernate merge抛出EntityNotFoundException

时间:2014-03-21 15:10:26

标签: java hibernate

我有一个Hibernate实体类代表"设备"表,链接到N deviceNetworks:

@Entity
    @Configurable  
    @Table(name = "device")
    public class Device implements Serializable
    {
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long id;


        @Column(name = "device_name", nullable = false)
        private String deviceName;


        @OneToMany( cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, 
                    mappedBy = "device_network", 
                    targetEntity = DeviceNetwork.class, orphanRemoval = true)
        private final Set<DeviceNetwork> deviceNetworks = Sets.newHashSet();

        ......

        public Set<DeviceNetwork> getDeviceNetworks()
        {
            return deviceNetworks;
        }

        public void addDeviceNetwork(final DeviceNetwork deviceNetwork)
        {
            deviceNetworks.add(deviceNetwork);
        }

        public void removeDeviceNetwork(final DeviceNetwork deviceNetwork)
        {
            deviceNetworks.remove(deviceNetwork);
        }
    }

和代表&#34; deviceNetwork&#34;的类:

@Entity
@Configurable
@Table(name = "device_network")
public class DeviceNetwork implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @ManyToOne(targetEntity = Device.class)
    @JoinColumn(name="device_id", nullable=false)
    private Device device;
    @Column(name = "network_name", unique = true, nullable = false)
    private String networkName;
    ......
}

我有一个将新设备和新链接的deviceNetwork插入数据库的事务,然后我开始使用新的tranaction:

  1. 通过其ID检索新插入的设备;
  2. 添加新网络;
  3. 更新设备;
  4. 我使用EntityManager来完成此任务:

    @Transactional
    public void addNetworkToDevice(long deviceId, DeviceNetwork deviceNetwork)
    {
        Device device = entityManager.get(deviceId); 
    
        device.addDeviceNetwork(deviceNetwork);
    
        entityManager.merge(device);
    }
    

    这会导致以下例外情况:

    org.springframework.orm.jpa.JpaObjectRetrievalFailureException: 
    Unable to find com.xxx.domain.DeviceNetwork with id 170; 
    nested exception is javax.persistence.EntityNotFoundException: 
    Unable to find com.xxx.domain.DeviceNetwork with id 170
    

    id = 170应该是要插入的DeviceNetwork。似乎merge()会尝试检索 即将插入的deviceNetwork由其自动生成的ID,但未能这样做,因为它还没有被插入。

    有趣的是,如果我在同一个交易中插入一个新设备和两个新的链接设备网络,它可以正常工作:

    @Transactional
    public void addDevice(Device device, DeviceNetwork deviceNetwork1, DeviceNetwork deviceNetwork2)
    {
        Device deviceInDB = entityManager.get(device.getDeviceId());
        if deviceInDB == null)
        {
            entityManager.persist(device);
            deviceInDB = entityManager.get(device.getDeviceId());
        }
    
        deviceInDB.addDeviceNetwork(deviceNetwork1);
        deviceInDB.addDeviceNetwork(deviceNetwork2);
    
        entityManager.merge(deviceInDB);
    }
    

    有什么建议吗?

1 个答案:

答案 0 :(得分:0)

检查项目中提到的数据库用户是否可以访问表device_network