Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Addtional Foreign Kry
Addtional Foreign Kry [message #699813] Fri, 22 July 2011 07:12 Go to next message
Michal  is currently offline Michal
Messages: 3
Registered: July 2011
Junior Member
Hi,
I am working on some project and have such situation:

I have Entity Service:

@Entity
public class Service implements Serializable {
  @Id
  private Long id;
  @ManyToMany
  Collection<Language> language;
}


And entity Language:

@Entity
public class Language implements Serializable {
  @Id
  @Column(length=4)
  private String id;
}


Thanks to it I have Entity Service which contains collection of available languages in DB it creates table: Service, Language and Service_Language for many to many mapping.

Now I want to add subscription entity:


@Entity
public class Subscription implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @ManyToOne(optional=false)
  private Service service;
  @ManyToOne
  private User user;

  @ManyToOne
  private Language language;
}


Now it creates table Subscription with service_id field which is foreign key to Service.id and field language_id which is foreign key to Language.id

OK but in this situation I can create subscription entity with some service id and language which is not on that service list.

I need one more foreign key contraint: foreign key (service_id,language_id) references Service_Language(service_id,language_id)

Ive tried some methods but it does not work and I cannot find anything in Google.

Maybe some of you can help me.

Cheers
Mike
Re: Addtional Foreign Kry [message #699955 is a reply to message #699813] Fri, 22 July 2011 11:58 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1018
Registered: July 2009
Senior Member
Hello Mike,

If you don't want to make object model changes, You can add the constraint to the database yourself, by either modifying the DDL or adding it in after the tables are created. ie:
<property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
as described here:
http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Using_EclipseLink_JPA_Extensions_for_Schema_Generation

The constraint though is not something managed in JPA because it is not mapped. That means the application will need to go to lengths to ensure it is respected - for instance, should the application need to disassociate a language from a service, it will also need to remove all Subscriptions and flush before removing the relationships. JPA will not know that subscriptions are dependent on the service-language relationship and may not remove them first otherwise. The same will need to be done on inserts; the service-language associations made and flushed before Subscriptions added for the same reasons.

The alternative would be to create an Entity for the Service_Language table with its pk being the composite (service_id,language_id). Then Subscription could map directly to it rather than to individual Services and Languages. The relationships would then be managed without need for breaking up transactions into parts, and the object model need not change too drastically - helper methods can be used to transparently return the service_Language components instead of service_language entities, if needed.

Best Regards,
Chris
(no subject) [message #699973 is a reply to message #699813] Fri, 22 July 2011 11:58 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1018
Registered: July 2009
Senior Member
Hello Mike,

If you don't want to make object model changes, You can add the constraint to the database yourself, by either modifying the DDL or adding it in after the tables are created. ie:
<property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
as described here:
http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Using_EclipseLink_JPA_Extensions_for_Schema_Generation

The constraint though is not something managed in JPA because it is not mapped. That means the application will need to go to lengths to ensure it is respected - for instance, should the application need to disassociate a language from a service, it will also need to remove all Subscriptions and flush before removing the relationships. JPA will not know that subscriptions are dependent on the service-language relationship and may not remove them first otherwise. The same will need to be done on inserts; the service-language associations made and flushed before Subscriptions added for the same reasons.

The alternative would be to create an Entity for the Service_Language table with its pk being the composite (service_id,language_id). Then Subscription could map directly to it rather than to individual Services and Languages. The relationships would then be managed without need for breaking up transactions into parts, and the object model need not change too drastically - helper methods can be used to transparently return the service_Language components instead of service_language entities, if needed.

Best Regards,
Chris
Re: Addtional Foreign Kry [message #704920 is a reply to message #699955] Fri, 29 July 2011 04:10 Go to previous message
Michal  is currently offline Michal
Messages: 3
Registered: July 2011
Junior Member
Hi Chris,
Thanks for reply. That gave me a clear hint that I should probably think about changing model a bit.

Regards,
Mike
Previous Topic:JPA EclipseLink unknown entity type issue
Next Topic:ConcurrencyManager deadlock
Goto Forum:
  


Current Time: Sat Aug 30 18:29:25 EDT 2014

Powered by FUDForum. Page generated in 0.01540 seconds