2

I have a unidirectional relation between two entities and I want to remove property object from join table only. I tried this but it only removes the property object from template and not from database join table. How can I remove it from join table:

template.getProperties().remove(property);

Entity calss:

        @Table(name = "template")
        public class GridTemplate {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "id_grid")
        @ManyToMany(cascade = CascadeType.ALL)
            @JoinTable(name = "grid_property",
                    joinColumns =
                    @JoinColumn(name = "id_grid", referencedColumnName = "id"),
                    inverseJoinColumns =
                    @JoinColumn(name = "id_property", referencedColumnName = "id"))
            private List<Property> properties;
    }

join table:

CREATE TABLE [dbo].[grid_property](
    [id_grid] [int] NOT NULL,
    [id_property] [int] NOT NULL,
 CONSTRAINT [PK_grid_property] PRIMARY KEY CLUSTERED 
(
    [id_grid] ASC,
    [id_property] ASC)
gabi
  • 1,324
  • 4
  • 22
  • 47
  • I don't understand the question : if it removes property element from the list in template object, it should remove it from join table when you flush it to DB ... you are saying that when you read back from DB, the property is read again and the list populated with the property you removed earlier ? – Pras Oct 12 '15 at 12:37
  • yes exactly, it doesn't remove them from database – gabi Oct 12 '15 at 12:39
  • What methods have you invoked from the EntityManager? Kindly also post the code to show exactly how you do the removal? – Ish Oct 12 '15 at 14:00
  • 1
    Just changing an entity won't do anything unless that entity is a managed instance, and then changes will only get synchronized to the database if the context is within a transaction and it commits or the entitymanager is flushed. Ensure you are merging your template and committing or flushing your em. – Chris Oct 13 '15 at 13:11
  • I was not calling flush on merge method of my Dao class. Thanks chris. – gabi Oct 14 '15 at 07:56

0 Answers0