0

In my project, akin to some sort of Amazon Prime service, an User (Id, first and last name) can rent (UserId and MovieId) a Movie (Id, title, director and length).

The project has this structure:

Source Packages/
├── Entity/
│   ├── Movies.java
│   ├── Rentals.java
│   ├── RentalsId.java
│   └── Users.java
├── Session/
│   ├── AbstractFacade.java
│   ├── MoviesFacade.java
│   ├── RentalsFacade.java
│   └── UsersFacade.java
└── View/
    ├── WebMovie.java
    ├── WebRental.java
    └── WebUser.java

Where the Entities all have variations of the following code (Except for RentalsID, which only has its two strings of ids and that's it):

@Entity
@Table(name = "xxx")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "xxx.findAll", query = "SELECT x FROM XXX x")
    , ....})
public class xxx implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    @Column(name = "id", length = 10, nullable = false)
    private String id;

    //getters, setters, other column data, equals, hash and tostring.

Except for Rentals, which also has @IdClass(RentalsId.class) and two @Id.

The XXXFacades extend from the AbstractFacade, and they only have an EntityManager, plus some methods to save, update and delete an entity. Here they are:

public void Save(x x, y y, z z) {
    try {
        xxx toSave = new xxx(x,y,z);
        entManager.persist(toSave);
    } catch (Exception e) {
        throw new EJBException(e.getMessage());
    }
}

public xxx Edit(xxx toChange) {
    try {
        return entManager.merge(toChange);
    } catch (Exception e) {
        throw new EJBException(e.getMessage());
    }
}

public void Delete(xxx toDelete) {
    try {   
        if (!entManager.contains(toDelete)) {
            toDelete = entManager.merge(toDelete);
        }

        entManager.remove(toDelete);
    } catch (Exception e) {
        throw new EJBException(e.getMessage());
    }
}

The WebXXX are there to communicate with the xhtml controller.

My issue is with Rentals. Since they have two primary, foreign keys, something happens in its facade that doesnt really works.

public void Delete(Rentals delRental) {
    System.out.println("FINDME");
    try {
        RentalsId id = new RentalsId(delRental.getIduser(), delRental.getIdmovie());
        Rentals emp = em.find(Rentals.class, id);
        System.out.println("name = " + emp.getIduser()+ " " + "movie = " + emp.getIdmovie());

        if (!em.contains(delRental)) {
            delRental = em.merge(delRental);
        }
        em.remove(delRental);
    } catch (Exception e) {
        throw new EJBException(e.getMessage());
    }
}

When I tried sending a null object through, it printed the FINDME and threw the exception. But when I sent through a Rental object, it didnt print anything, not even the exception. I dont really know of its entering or not, because since its not throwing errors, it should be in, but since its not printing my FINDME flag, I dont think it's even entering the method.

How can i solve this? Does it have anything to do with the RentalId object? Do i have to use it in some way? Or am I simply using the remove method incorrectly?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Frank Vargas
  • 31
  • 1
  • 7
  • 'But when I sent through a Rental object, it didnt print anything, not even the exception' - could you please elaborate on that? Did you debug your code, stepped over the call to `rentalsFacade.Delete`, and nothing happened? – crizzis Jul 11 '19 at 09:44
  • @crizzis i tried to debug the program by putting the breakpoints at the calls of the Web and Facade's delete methods. When i just send a null object, the debbuging goes through and i can see how it prints the FINDME strings. But when i send through an object from a list, which its how they're being displayed, it doesnt even enter the web's delete method. Moving things around and deleting from the delete.xhtml the command ' onclick="window.location.reload();"' in the delete button shows this error: Conversion Error setting value 'Entity.Rentals@4b0cde1' for 'null Converter' – Frank Vargas Jul 15 '19 at 16:40
  • Well then, the problem lies in the presentation layer, it has nothing to do with persistence. [This question](https://stackoverflow.com/questions/4734580/conversion-error-setting-value-for-null-converter-why-do-i-need-a-converter) looks strikingly similar to the issue you're having – crizzis Jul 16 '19 at 09:30
  • @crizzis the page had something similar to my error, and I managed to solve it thanks to it! It was mostly a converter issue, since i started without one, and that the Rentals java had an error on the Equals method. Thanks for the help! – Frank Vargas Jul 16 '19 at 15:44

0 Answers0