sql - Play Framework: JPA one-to-many relationship between three entities? -
I am creating a web application using the play framework and I have problems creating database models using this schema I'm running in
** Client ** - (many many) - ** events ** / / (many from one) (multiple to one) \ / ** Comments **
I am able to create and add relationships
event event = new event ("party"); Client Client = New Client ("test@gmail.com", "fname", "lname", "street", "phononam"); Comment Comment = New Comment ("Comment!"); // Relationships add client.addEvent (event); Event.addClient (customer); Event.add Comment (comment); Comment.addEvent (event); Event.addClient (customer); Client.add Comment (comment);
But when I try to continue with the data
client.save (); Event.save (); Comment.save ();
I get an error
[error] test commentModelTest.Test ClientAventment failed: javax.preistence.PersistenceException: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "Public at PRIMARY_KEY_4A. EVENT_CLIENT (EVENT_ID, CLIENT_EMAIL)"; SQL statement: [Error] Below is the definition of the class in the event_client (client_email, event_id) values (?,?) [23505-172]
, I really appreciate it If someone can help me solve this issue, am I defining the relationship incorrectly? thank you in advanced!
Client class:
Enty public class client model extension {@Id public string email; @ Essential public string first name; @ Essential public string lastName; Public string homeorder; Public string phone number; @ManyToMany (mapped = "client", cascade = cascade type.AL) public list & lt; Event & gt; Incidents = New Arrestist & lt; Event & gt; (); @OneToMany (mapped = "client", cascade = cascade type.AL) public list & lt; Comment & gt; Comment = New Arrestist & lt; Comment & gt; ();
Event class:
Entee public class event model extension {@Id Public Long ID; @ Essential public string event name; @ManyToMany (cascade = CascadeType.ALL) public list & lt; Client & gt; Client = New Arrestist & lt; Client & gt; (); @OneToMany (mapped = "event", cascade = cascade type.AL) public list & lt; Comment & gt; Comment = New Arrestist & lt; Comment & gt; ();
Comment class:
Enty public class comment model extension {@ id public long ID; @ Essential public string message; @ManyToOne (Cascade = Cascade Type. PERSIST) Public Client Client; @ManyToOne (Cascade = Cascade Type. ParsiST) Public Event Event;
--- Edit: -------------------------------
I realized where I went wrong If you are running in a similar error, check if you are using the model correctly! In my JUnit test, I tried to add the same client twice to an event, so that the primary key (email) can be repeated.
The correct connection code should be as follows:
client.addEvent (event); Event.addClient (customer); Event.add Comment (comment); Comment.addEvent (event); Comment.addClient (customer); Client.add Comment (comment); The exception says that you are trying to add the same relationship twice. "Public events at PRIMARY_KEY_4A" (EVENT_ID, CLIENT_EMAIL) "
I think this
event
and, so it maps to many relations.
> Client
client.addEvent (event); here -> event.addClient (Customer); event.add comment (comment); comment.addEvent (event); here -> event.addClient (customer); client.add Comment (comment);
The bar has been added and there is a unique index that prevents it.
Comments
Post a Comment