1

Here are the entity snippet.......Ignore DaoObject..it just adds autogenerated Id to each entity it subclasses.

I've highlighted which is necessary to persist Content. Eventullally it is not able to add value to contentid column in streams table. It though adds a row to both content and streams table with below code..

Please check and help me troubleshoot the problem

          Content newContent = TestHelper.contentFactory(null, "streamForCREATE_IT", xxxx, 100);
          Stream stream = TestHelper.streamFactory(null, "name", "appname",);
          **stream.setContent(newContent);
          List<Stream> streams = new ArrayList<Stream>();
          streams.add(stream);
          newContent.setStreams(streams);**


@Entity
public class Content extends DaoObject {
  public Content() {
  };

  private String name;
  private ContentType type;
  @OneToMany(fetch = FetchType.EAGER, mappedBy = "content", cascade = {CascadeType.PERSIST,CascadeType.MERGE}, orphanRemoval = true)
  @JsonManagedReference
  private List<Stream> streams = new ArrayList<Stream>();
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public List<Stream> getStreams() {
    return streams;
  }
  public void setStreams(List<Stream> streams) {
    this.streams = streams;
  }
    public void addStream(Stream newStream) {
    if (streams == null) {
      streams = new ArrayList<Stream>();
    }
    newStream.setContent(this);
    streams.add(newStream);
  }
}

@Entity
public class Stream extends DaoObject {
  public Stream() {
  }
  @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinColumn(name = "contentid")
  @JsonBackReference
  private Content content;**
  private String name;
  private String appName;
  public Content getContent() {
  return content;
  }
  public void setContent(Content content) {
  this.content = content;
  }
  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  public String getAppName() {
  return appName;
  }
  public void setAppName(String appName) {
  this.appName = appName;
  }
} 

Here is the image from mysql workbench.

enter image description here

Ketan Khairnar
  • 1,620
  • 3
  • 12
  • 21
  • You did not show how/what you persist. – V G Sep 22 '14 at 14:38
  • @AndreiI I've added screenshot for reference. Note that Entities in the code added above don't have redundant columns – Ketan Khairnar Sep 22 '14 at 15:17
  • What I mean is: somewhere you call JPA.em() and then `persist`. Could you show us that part? – V G Sep 22 '14 at 15:21
  • dao is injected in JavaService @ Override public void saveNew(DaoObject elt) { JPA.em().persist(elt); } @ Override public JavaServiceResult createContent(Content content) throws ServiceException { if (content.getName() == null || content.getType() == null || content.getCpm() == 0) { throw new ServiceException(UserCreateServiceJavaImpl.class.getName(), BAD_REQUEST, "Mandatory Field(s) missing!", Messages.get("content.create.error.mandatory.fields.missing")); } dao.saveNew(content); return JavaServiceResult.buildServiceResult(CREATED, content); } – Ketan Khairnar Sep 22 '14 at 15:42
  • Wow this is real weird. In my test class I did call set on either side to have the relation available in persisted. It did not work. – Ketan Khairnar Sep 23 '14 at 06:43

1 Answers1

0

Damn it was stupid. Am testing REST API where this is happening and I did set streams for content and each stream's content correctly but that was all happening before JSON transformation. When this association is recreated in dao it worked. Thanks @AndreiI for asking correct questions which helped me resolve this.

Ketan Khairnar
  • 1,620
  • 3
  • 12
  • 21