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?