为什么entityManager.remove不起作用?

时间:2017-11-03 13:14:48

标签: java hibernate entitymanager

我的项目中的特定情况有点问题。 我有一个名为SupplierDocument的实体,我会在其中删除一条记录。

在下面的类中,我从documentService调用remove方法

@ViewScope
@SpringView(name = Test.VIEW_NAME)
public class Test extends VerticalLayout implements View {
/**
 *
 */
private static final long serialVersionUID = 1L;

public static final String VIEW_NAME = "test";

@Autowired
private DocumentService documentService;


@PostConstruct
void init() {
    Button delete = new Button(VaadinIcons.TRASH);
    delete.addClickListener(e -> deleteSupplierDocument());
    addComponent(delete);
}

@Override
public void enter(ViewChangeEvent event) {
    // This view is constructed in the init() method()
}


private void deleteSupplierDocument() {

    try {

        SupplierDocument sd = documentService.getSupplierDocumentById(1L);
        documentService.remove(sd);
    }

    catch (Exception e) {

    }
}

}

但db中的行仍然存在。为什么???

此处涉及的实体:

SupplierDocument.java

@Entity
@Table(name = "supplierDocument")
public class SupplierDocument implements Serializable {

/**
 *
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@ManyToOne
@JoinColumn(name = "requiredDocument")
private RequiredDocument requiredDocument;

@ManyToOne
@JoinColumn(name = "supplier")
private Supplier supplier;

@ManyToOne
@JoinColumn(name = "fileReference")
private FileReference fileReference;

@Enumerated(EnumType.STRING)
@Column(name = "status")
private UploadStatus status;

// here getter and setter 

}

Supplier.java

@Entity
@Table(name = "supplier")
public class Supplier implements Serializable {

/**
 *
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@Column(name = "businessName")
private String businessName;

@OneToMany(fetch=FetchType.EAGER, mappedBy = "supplier")
@Cascade(value = CascadeType.ALL)
private List<SupplierDocument> documents;

@Column(name = "notes")
private String notes;

// here getter and setter

}

DocumentService.java

@Service
public class DocumentService {

@PersistenceContext
protected EntityManager entityManager;

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

@Transactional
public <T> T remove(final T o) {
    try {
        T det = o;
        if (!entityManager.contains(o)) {
            det = entityManager.merge(o);
        }
        entityManager.remove(det);
        //entityManager.flush();
        return det;
    } finally {
        //entityManager.close();
    }
}

public EntityManager getEntityManager() {
    return entityManager;
}

public CriteriaBuilder getCriteriaBuilder() {

    return entityManager.getCriteriaBuilder();
}

}

持久性配置是:

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="it.myProject" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property> 
</bean>

问题可能是映射?我读了一些关于它的内容,但我现在还没有找到解决方案。

请帮帮我。

由于

0 个答案:

没有答案