public void create(SensorInfo sensorInfo) throws PreexistingEntityException, Exception {
        if (sensorInfo.getAlarmInfoCollection() == null) {
            sensorInfo.setAlarmInfoCollection(new ArrayList<AlarmInfo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CustomerInfo relatedCustomerId = sensorInfo.getRelatedCustomerId();
            if (relatedCustomerId != null) {
                relatedCustomerId = em.getReference(relatedCustomerId.getClass(), relatedCustomerId.getCustomerId());
                sensorInfo.setRelatedCustomerId(relatedCustomerId);
            }
            Collection<AlarmInfo> attachedAlarmInfoCollection = new ArrayList<AlarmInfo>();
            for (AlarmInfo alarmInfoCollectionAlarmInfoToAttach : sensorInfo.getAlarmInfoCollection()) {
                alarmInfoCollectionAlarmInfoToAttach = em.getReference(alarmInfoCollectionAlarmInfoToAttach.getClass(), alarmInfoCollectionAlarmInfoToAttach.getSerialNumber());
                attachedAlarmInfoCollection.add(alarmInfoCollectionAlarmInfoToAttach);
            }
            sensorInfo.setAlarmInfoCollection(attachedAlarmInfoCollection);
            em.persist(sensorInfo);
            if (relatedCustomerId != null) {
                relatedCustomerId.getSensorInfoCollection().add(sensorInfo);
                relatedCustomerId = em.merge(relatedCustomerId);
            }
            for (AlarmInfo alarmInfoCollectionAlarmInfo : sensorInfo.getAlarmInfoCollection()) {
                SensorInfo oldRelatedSensorIdOfAlarmInfoCollectionAlarmInfo = alarmInfoCollectionAlarmInfo.getRelatedSensorId();
                alarmInfoCollectionAlarmInfo.setRelatedSensorId(sensorInfo);
                alarmInfoCollectionAlarmInfo = em.merge(alarmInfoCollectionAlarmInfo);
                if (oldRelatedSensorIdOfAlarmInfoCollectionAlarmInfo != null) {
                    oldRelatedSensorIdOfAlarmInfoCollectionAlarmInfo.getAlarmInfoCollection().remove(alarmInfoCollectionAlarmInfo);
                    oldRelatedSensorIdOfAlarmInfoCollectionAlarmInfo = em.merge(oldRelatedSensorIdOfAlarmInfoCollectionAlarmInfo);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findSensorInfo(sensorInfo.getSensorId()) != null) {
                throw new PreexistingEntityException("SensorInfo " + sensorInfo + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }