0

I have a weird problem with remote EJB. Here is an entity:

@Entity
@Table(name="cd")
@AttributeOverrides({
        @AttributeOverride(name = "id", column = @Column(name = "cd_id")),
        @AttributeOverride(name = "title", column = @Column(name = "cd_title")),
        @AttributeOverride(name = "description", column = @Column(name = "cd_description"))
})
@NamedQueries({
        @NamedQuery(name = "findAllCDs", query = "SELECT cd FROM CD cd")
})
public class CD  extends Item  implements Serializable{


    private String musicCompany;
    private Integer numberOfCDs;
    private Float totalDuration;
    private String genre;

     public CD(String title, Float price,String description, String musicCompany,
          Integer numberOfCDs, Float totalDuration, String genre) {
    super(title, price, description);
    this.musicCompany = musicCompany;
    this.numberOfCDs = numberOfCDs;
    this.totalDuration = totalDuration;
    this.genre = genre;
}

And mapped superclass:

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Item {

    @Id
    @GeneratedValue
    protected Long id;

    @NotNull
    @Column(name="title",nullable = false,updatable = false)
    protected String title;

    protected Float price;

    @Size(min = 10,max=2000)
    @Column(length = 2000)
    protected String description;

I am trying to persist it using the Following ejb:

@Stateless
@LocalBean
public class ItemEJB implements ItemLocal,ItemRemote{

    @PersistenceContext(unitName = "demo_unit")
    private EntityManager em;

    @Override
    public List<Book> findBooks() {
        TypedQuery<Book> query = em.createNamedQuery("findAllBooks", Book.class);
        return query.getResultList();
    }

    @Override
    public List<CD> findCDs() {
        TypedQuery<CD> query = em.createNamedQuery("findAllCDs", CD.class);
        return query.getResultList();
    }

    @Override
    public Book createBook(Book book) {
        em.persist(book);
        return book;
    }

    @Override
    public CD createCD(CD cd) {
        em.persist(cd);
        return cd;
    }
}

The problem is with this remote interface:

@Remote
public interface ItemRemote {
    List<Book> findBooks();
    List<CD> findCDs();
    Book createBook(Book book);
    CD createCD(CD cd);
}

When I am trying to persist an Entity like this:

  CD cd = new CD("Love SUpreme", 20f, "John Coltrane love moment",
                "Blue Note", 2, 87.45f, "Jazz");

        cd = itemRemote.createCD(cd);

I am getting

ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=title, rootBeanClass=class org.abondar.experimental.javaeedemo.ejbdemo.model.CD, messageTemplate='{javax.validation.constraints.NotNull.message}'}

When I am doing the same with local or just stateless ejb everything works fine. The problem is exactly with remote one. What may I have misconfigured?

Alex Bondar
  • 1,167
  • 4
  • 18
  • 35
  • You have an `@NotNull` annotation on some class, and something is NULL when you try to persist it. That is the Bean Validation API, not the JPA API. Debugging would tell you why it is null –  Nov 22 '17 at 18:35
  • There is no null values as you can see how I persist. Yes there is NotNull annotation – Alex Bondar Nov 22 '17 at 21:13
  • We can only see how you persist. We can't see how you set your fields, your `CD` constructor is missing from the question – ytg Nov 23 '17 at 09:19
  • Added a constructor for It – Alex Bondar Nov 23 '17 at 13:00
  • Aaand you still don't show setting your only `@NotNull` field, the `title`, because it's set in the `Item` constructor :) – ytg Nov 24 '17 at 08:48
  • Yes, but CD calls super – Alex Bondar Nov 24 '17 at 11:10
  • The problem is with extending an Item class. I have moved fields from Item to CD entity and every seems to be working – Alex Bondar Nov 24 '17 at 21:07

1 Answers1

0

After almost giving up I have found a solution. Method createCD from ItemRemote EJB should be implemented like this:

 @Override
    public CD createCD(Item cd) {
        em.persist(cd);
        return em.find(CD.class,cd.getId());
    }

The change is that the type of cd parameter is Item but not CD which extends Item class.In such case there are no problems with validation

Alex Bondar
  • 1,167
  • 4
  • 18
  • 35