1

I have many entities and for CUD operations i use persist(), merge() and remove() methods, respectively. But delete method below did not work. So i had to use query for deleting a given object. I have googled it but could not find any satisfactory answer.

@Stateless
@LocalBean
public class PickListFacade {

    @PersistenceContext
    private transient EntityManager  em;

    // ...

    public boolean deletePickList(final PickList pickList) {
        try {
            // below commented out line did not work; so one below that is employed
            // this.em.remove(this.em.find(PickList.class, pickList.getId()));
            this.em.createQuery("DELETE FROM PickList p WHERE p.id = :id").setParameter("id", pickList.getId()).executeUpdate();
            return true;
        } catch (final PersistenceException pe) {
            logger.error("[deletePickList] : Error : {}", pe.getMessage(), pe);
            return false;
        }
    }
}

For the sake of simplification, i replaced PickList with Tuple. But did not think that it would be a source of confusion. So here it is: the original code...

@Entity
@Table(name = "PICK_LIST_VALUES")
public class PickListValue implements Serializable, ILabelValue {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "list_value_generator")
    @TableGenerator(initialValue = 1, 
                    allocationSize = 1, 
                    name = "list_value_generator", 
                    table = "SEQUENCES", 
                    pkColumnName = "SEQUENCE_NAME", 
                    valueColumnName = "SEQUENCE_NEXT_HI_VALUE", 
                    pkColumnValue = "PICK_LIST_VALUES")
    @Column(name = "PICK_LIST_VALUE_ID")
    private Long              id;

    @Column(name = "PICK_LIST_VALUE")
    private String            value;

    @Column(name = "PICK_LIST_VALUE_CODE")
    private Long              code;

    @Column(name = "RANK")
    private Integer           rank;

    // bi-directional many-to-one association to PickList
    @ManyToOne
    @JoinColumn(name = "PICK_LIST_ID")
    private PickList          pickList;

    public PickListValue() {
    }

    // getters and setters ...
}

@Entity
@Table(name = "PICK_LISTS")
public class PickList implements Serializable {

    private static final long   serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "list_generator")
    @TableGenerator(initialValue = 1,
                    allocationSize = 1,
                    name = "list_generator",
                    table = "SEQUENCES",
                    pkColumnName = "SEQUENCE_NAME",
                    valueColumnName = "SEQUENCE_NEXT_HI_VALUE",
                    pkColumnValue = "PICK_LISTS")
    @Column(name = "PICK_LIST_ID")
    private Long                id;

    @Column(name = "PICK_LIST_NAME", unique = true)
    private String              name;

    @Column(name = "PICK_LIST_DESCRIPTION")
    private String              description;

    @Column(name = "CREATED_BY")
    private String              createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DATE_CREATED")
    private Date                dateCreated;

    @OneToMany(mappedBy = "pickList", fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true)
    private List<PickListValue> pickListValues;

    public PickList() {
    }

    // getters and setters ...
}
Ahmet
  • 908
  • 1
  • 17
  • 26
  • 1
    What does it do when you say it does not work? Does it throw an exception? Is your primary key correctly defined on your Tuple? – Fradenger Jun 04 '14 at 17:06
  • Id column is constructed as my other entity's. persist() and merge() methods work w/o problem. remove() method does not give any exception. – Ahmet Jun 04 '14 at 17:11
  • Just doing nothing would indeed be surprising assuming container managed persistence (ie, implicit commit at the end of `deleteTuple`). – mabi Jun 04 '14 at 19:44
  • I have posted the original code. Sorry for the confusion. And yes i just rerun the code: it does not write any exception to the console. I am doing what i have to do using the query way but the reason i am asking this question here is for the community to find a solution to this -possible- bug of Hibernate. – Ahmet Jun 05 '14 at 05:25
  • @Ahmet Do you have `@Transactional` in your class? And if trace logging is not enabled, please enable it to see database sql statements. – Jacob Jun 05 '14 at 08:01
  • @Polppan, i have enabled "hibernate.show_sql" to see the queries and did not see any relevant statements. – Ahmet Jun 05 '14 at 11:27
  • @Ahmet You mean to say you cannot see any sql statements? – Jacob Jun 05 '14 at 11:30
  • Can you post your table mapping? – Fradenger Jun 05 '14 at 13:18

0 Answers0