John
John

Reputation: 85

Unidirectional @OneToMany with @JoinTable not updating the join table

I'm trying to achieve unidirectional one to many mapping using Spring Boot, JPA, QueryDsl and in memory H2 database. When I insert data through schema.sql file relation is properly retrieved. Issue is when storing a new object to Event table.

Schema.sql

CREATE TABLE tag (
  tag_id  INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
  label   VARCHAR(25)
);

CREATE TABLE event (
  event_id       INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
  owner_id       INTEGER NOT NULL,
  place_id       INTEGER NOT NULL,
  content        TEXT,
  thumbnail      VARCHAR(100),
  heading        VARCHAR(50),
  date_added     DATE,
  start_date     DATE,
  start_time     TIME,
  end_date       DATE,
  end_time       TIME,
  approved       BOOL,
  FOREIGN KEY (owner_id) REFERENCES user (user_id),
  FOREIGN KEY (place_id) REFERENCES place (place_id)
);

CREATE TABLE event_tag (
  event_id   INTEGER NOT NULL,
  tag_id     INTEGER NOT NULL,
  PRIMARY KEY (event_id,tag_id),
  KEY fk_event (event_id),
  KEY fk_tag (tag_id),
  CONSTRAINT fk_tag FOREIGN KEY (tag_id) REFERENCES tag (tag_id),
  CONSTRAINT fk_event FOREIGN KEY (event_id) REFERENCES event (event_id)
)

Event.java

@Entity
@Getter
@Setter
@EqualsAndHashCode
@Table(name = "event")
public class Event implements Serializable {
    @Id
    @GeneratedValue
    @Column(name = "event_id")
    protected Long id;

    ...

    //todo: not working while saving
    @OneToMany(cascade=CascadeType.ALL)
    @JoinTable(
            name = "event_tag",
            joinColumns = @JoinColumn(name = "event_id"),
            inverseJoinColumns = @JoinColumn(name = "tag_id")
    )
    private Set<Tag> tags;

    ...
}

Tag.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode
@Table(name = "tag")
public class Tag implements Serializable {
    @Id
    @GeneratedValue
    @Column(name = "tag_id")
    private Long id;

    @Column(name = "label")
    private String label;
}

And then this is how I store the Event in the service EventService.java

public Optional<Event> create(Event event) {
    try {
        entityManager.unwrap(Session.class).save(event);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return Optional.ofNullable(event);
}

At this point the relation between the Tag and Event is present with the Event object Tags are related to the Event Object

I never get any error. After the object is saved by the session the joining table remains unmodified. Event table gets extended with newly stored object.

Also my application.properties:

spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.h2.console.enabled=true

What am I missing ? Any help appreciated. Thank you!

Upvotes: 0

Views: 1281

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36103

You don't execute flush() after save so no SQL statements are generated.

Either call flush() or make sure that create() is executed in a transaction context.

Upvotes: 1

Related Questions