So this question has been asked so many times but my problem is a little different. I have 2 classes "PlaceType" and "Places". PlaceType class is for objects which represents the type of Place like "restaurants, clubs, hotesl etc". Each object of the type "Place" will have a placeType. But the existance of these two is not related to each other. i.e when i delete the Place the PlaceType must not be deleted in the Database. I am using Eclipselink. The code for both classes is given below.
@Entity
@Table(schema = "itmd4515", name = "placeType", uniqueConstraints = @UniqueConstraint(columnNames = "placeTypeName"))
public class PlaceType implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 255, unique = true)
@Size(max = 255, message = "The type of the Place must not be more than 255 characters")
@NotNull
private String placeTypeName;
@Size(max = 2500, message = "The Description should be no more than 2500 characters")
@NotNull
@Column(name = "placeTypeDesc", nullable = false, updatable = true)
private String placeTypeDesc;
And the class for places is given below
@Entity
@Table(schema = "itmd4515", name = "places", uniqueConstraints = @UniqueConstraint(columnNames = "locationOfThePlace"))
public class Places implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@NotNull
@OneToOne(cascade = CascadeType.PERSIST)
private PlaceType typeOfPlace;
The Code which deletes the data in the Database is given below.
public boolean delete(Object id) {
try {
tx.begin();
T entityToBeRemoved = em.getReference(entityClass, id);
LOG.info("The Entity to be removed"+entityToBeRemoved.toString());
em.remove(entityToBeRemoved);
tx.commit();
return true;
} catch (Exception ex) {
LOG.info("There was an exception while deleting the object" + entityClass.getName() + ex.getMessage());
tx.rollback();
return false;
}
}
Now in jUnit test Case i create a test object for both the PlaceType and Places class, insert it and then delete it. Insert is working but the delete is not working for some reason. I tried to use Cascade.REMOVE, but still does not work. Could someone please let me know what i am doing wrong?
Also i am trying to delete just the "Places" and not the "PlaceType" in the database. because a single PlaceType can be shared by many "Places". I am sure i have done something wrong in the design. It would be very helpful if someone pointed out the right way to do this. Thanks.