Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPA don't check if the entity already exists in the database

843830Jul 12 2010 — edited Jul 13 2010
Hello,

From inside a MDB Ι persist movie entities which has manyToMany reliationship with entity actor

em.persist(movie);

because in the movie entity there are the annotations:

@ManyToMany(cascade=CascadeType.PERSIST)
@JoinTable(name="ACTOR_MOVIE", joinColumns={@JoinColumn(name="title")}, inverseJoinColumns={@JoinColumn(name="actor_name")})
private Set<Actor> actors = new TreeSet<Actor>();

also the actors of the movie also persisted

but if in the database there is already the movie's actor I receive a sheet of exception!

I make use of:

NetBeans IDE 6.7.1
GlassFish v2.1
Oracle 10g XE
JAVA 6 SE
JAVA 5 EE
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
the movie Entity (I know the title it is not a proper primary key but I just want a simple database to use with my first JEE project):

@Entity
@Table(name="movie")
public class Movie implements Serializable
{
@Id
@Column(name="title")
private String title;

@Column(name="production_year")
private int productionYear;

@Enumerated(EnumType.STRING)
@Column(name="rating")
private Ratings rating;



@ManyToMany(cascade=CascadeType.PERSIST)
@JoinTable(name="ACTOR_MOVIE", joinColumns={@JoinColumn(name="title")}, inverseJoinColumns={@JoinColumn(name="actor_name")})
private Set<Actor> actors = new TreeSet<Actor>();

@ManyToMany(cascade= CascadeType.PERSIST)
@JoinTable(name="DIRECTOR_MOVIE", joinColumns={@JoinColumn(name="title")}, inverseJoinColumns={@JoinColumn(name="director_name")})
private Set<Director> directors = new TreeSet<Director>();


public String getTitle()
{
return title;
}

public void setTitle(String title)
{
this.title = title;
}

public int getProductionYear()
{
return productionYear;
}

public void setProductionYear(int productionYear)
{
this.productionYear = productionYear;
}

public Ratings getRating()
{
return rating;
}

public void setRating(Ratings rating)
{
this.rating = rating;
}



public Set<Actor> getActors()
{
return actors;
}

public void setActors(HashSet<Actor> actors)
{
this.actors = actors;
}

public Set<Director> getDirectors()
{
return directors;
}

public void setDirectors(HashSet<Director> directors)
{
this.directors = directors;
}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------
the Actor entity:

@Entity
@Table(name="actor")
public class Actor implements Serializable, Comparable<Actor>
{
@Id
@Column(name="actor_name")
private String actorName;

public String getActorName()
{
return actorName;
}

public void setActorName(String actorName)
{
this.actorName = actorName;
}

public Actor()
{
}

public Actor(String actorName)
{
this.actorName = actorName;
}

public int compareTo(Actor o)
{
return this.getActorName().compareToIgnoreCase(o.getActorName());
}
}
-------------------------------------------------------------------------------------------

part of the GlassFish v2.1 output
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 10 2010
Added on Jul 12 2010
3 comments
1,084 views