JPA: create constraint between primary key column in one table and a non-primary column in another? - jpa

I’m using JPA and trying to figure out how to create a unique constraint between a primary key column in one table and a non-primary key column in another. I have two tables:
Customer (
id character varying(32) NOT NULL,
customer_name character varying(50)
)
and
Account (
id character varying(32) NOT NULL,
account_name character varying(50)
)
There is a unidirectional, one-to-many relationship between Customer and Account with a constraint that Account.account_name is unique per Customer. The code looks like so:
#Entity
public class Customer {
#Id
#Column(length=32)
private String id;
#Column(unique=true, length=50)
private String customer_name;
#OneToMany
private List<Account> accounts;
...
}
and
#Entity
public class Account {
#Id
#Column(length=32)
private String id;
#Column(length=50)
private String account_name;
...
}
A join table is created by default:
CUSTOMER_ACCOUNTS (
customer_id character varying(32),
accounts_id character varying(32)
)
How do I create the unique constraint to ensure an Account.account_name is unique per Customer?

Add a JoinColumn annotation to the Customer class::
#Entity
public class Customer {
#Id
#Column(length=32)
private String id;
#Column(unique=true, length=50)
private String customer_name;
#OneToMany(mappedBy="customer")
#JoinColumn(name="CUSTOMER_ID", referencedColumnName="ID")
private List<Account> accounts;
...
}
Referenced column name points to the primary key of the Customer table, while name points to the foreign key field of the Account table.
You should also add a CUSTOMER_ID field and a composite unique constraint to the account entity:
#Entity
#Table(
uniqueConstraints=
#UniqueConstraint(columnNames={"CUSTOMER_ID", "ACCOUNT_NAME"})
)
public class Account {
#Id
#Column(length=32)
private String id;
#Column(length=50)
private String account_name;
#Column(name="CUSTOMER_ID")
private String customer_id;
...
}

Related

Persist One to One Relation with Postgres Sequence

I'm playing arround with JavaEE in my free time since a few Months
I have 2 Tables in Postgres
Table person
create table person(id bigint primary key default nextval('person_id_seq'), persontype varchar(5), name varchar(50), ERP_id smallint);
Table address
create table address (customer_id bigint primary key NOT NULL references person(id), department varchar(50), street varchar(50), plz varchar(10), city varchar(50), country varchar(50));
Model person
public class Person implements Serializable {
#Id
#SequenceGenerator(
name="person_id_seq",
sequenceName="person_id_seq",
allocationSize=1,
initialValue=1
)
#GeneratedValue(
strategy=GenerationType.AUTO,
generator="person_id_seq"
)
private Long id;
private char personType;
private String name;
private Long erp_id;
#OneToOne (cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
#JoinColumn(name="id", insertable=false, updatable=false)
private Address address;
[...]
#PrePersist
public void initializeCustomerId(){
this.address.setCustomerId(id);
}
Model address
public class Address implements Serializable {
#Id
#GeneratedValue(
strategy=GenerationType.AUTO)
#Column(name="person_id")
private Long personId;
private String department;
private String street;
private int plz;
private String city;
private String country;
I've got this solution from https://blogs.oracle.com/WebLogicServer/entry/ejb3_mapping_of_one-to-one_rel
My Problem now... before I persist I need the id which will be generated by the Postgres-Sequence.
My Thought on this: in #PrePersist I could do an
select nextval('person_id_seq');
BUT What if there are more Clients who wants to save at the same time!!
So, how could I solve this Issue? Or am I completely wrong on this?
Thanks
Paddie

jpa composite primary key and jointable with unique key

here are the sql tables:
option primary key is name + value
CREATE TABLE `option` (
id int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`value` varchar(45) NOT NULL,
PRIMARY KEY (`name`, `value`),
UNIQUE KEY `id_UNIQUE` (`id`)
)
product primary key is incremental id
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
)
a product has several options (referenced by unique key "id")
CREATE TABLE product_option (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_product` int(11) NOT NULL,
`id_option` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `id_product` (`id_product`),
KEY `id_option` (`id_option`),
CONSTRAINT `FK_product_option` FOREIGN KEY (`id_product`) REFERENCES `product` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_option_product` FOREIGN KEY (`id_option`) REFERENCES `option` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
On java side, i've mapped "option" this way
#Embeddable
public class OptionId implements Serializable{
#Column(name="value")
private String value;
#Column(name="name")
private String name;
}
#Entity #Table(name="option")
public class Option {
#Column(name="id", unique=true)
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#EmbeddedId
private OptionId primaryKey;
public OptionId getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(OptionId primaryKey) {
this.primaryKey = primaryKey;
}
}
and product this way
#Entity
#Table(name="product")
public class Product {
#Id #Column(name="id")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#Column(name="name")
private String name
#OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
#JoinTable(
name="product_option",
joinColumns = #JoinColumn( name="id_product"),
inverseJoinColumns = #JoinColumn( name="id_option", referencedColumnName="id")
)
private Set<Option> options;
}
then at execution time i get this error
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at org.hibernate.sql.SelectFragment.addColumns(SelectFragment.java:107)
at org.hibernate.persister.collection.BasicCollectionPersister.manyToManySelectFragment(BasicCollectionPersister.java:308)
at org.hibernate.persister.collection.BasicCollectionPersister.selectFragment(BasicCollectionPersister.java:294)
at org.hibernate.loader.JoinWalker.selectString(JoinWalker.java:1070)
at org.hibernate.loader.AbstractEntityJoinWalker.initStatementString(AbstractEntityJoinWalker.java:124)
at org.hibernate.loader.AbstractEntityJoinWalker.initStatementString(AbstractEntityJoinWalker.java:109)
at org.hibernate.loader.AbstractEntityJoinWalker.initAll(AbstractEntityJoinWalker.java:91)
at org.hibernate.loader.AbstractEntityJoinWalker.initAll(AbstractEntityJoinWalker.java:78)
at org.hibernate.loader.entity.CascadeEntityJoinWalker.<init>(CascadeEntityJoinWalker.java:52)
at org.hibernate.loader.entity.CascadeEntityLoader.<init>(CascadeEntityLoader.java:47)
at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3254)
at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:3191)
at org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:728)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 60 more
if i remove "referencedColumnName", of course i get the error
A Foreign key refering Option from Product has the wrong number of column. should be 2
but i can't figure out how to fix this
Based on your schemas, your entity annotations are slightly off. Where you specify 'product' and 'option' you should be using 'id_product' and 'id_option':
#OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
#JoinTable(
name="product_option",
joinColumns = #JoinColumn( name="id_product"),
inverseJoinColumns = #JoinColumn( name="id_option", referencedColumnName="id")
)
private Set<Option> options;
As an aside, your life will be considerably easier, in the long run, if you switch the primary and unique keys on your option table.

Declare JPA entity for a tree element

I do have a DB table with unidirectional trees. Leafs of these trees can have several children/parents.
Cycles are restricted.
Here is my DB table definition:
CREATE MULTISET TABLE WORKFLOW_SEQ_REL ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
WORKFLOW_SEQ_ID INTEGER NOT NULL,
REL_WORKFLOW_SEQ_ID INTEGER NOT NULL,
JOB_ID BIGINT)
PRIMARY INDEX ( WORKFLOW_SEQ_ID );
As you can see it doesn't have a primary key right now. But it would appear later :) Really PK is: JOB_ID+PARENT_ID+CHILD_ID.
The idea is:
REL_WORKFLOW_SEQ_ID = PARENT
WORKFLOW_SEQ_ID = CHILD
JOB_ID = TREE_IDENTIFICATOR (a determinant to separate different trees stored
in one table).
I'm trying to declare a JPA entity:
#Entity
#Table(name="WORKFLOW_SEQ_REL")
public class EtlWorkflowSeqNode {
#EmbeddedId
public EtlWorkflowSeqNodeId etlWorkflowSeqNodeId;
//#Column(name="JOB_ID")
//public Integer jobId;
#Embeddable
class EtlWorkflowSeqNodeId{
#Column(name="JOB_ID")
public Integer jobId;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name="REL_WORKFLOW_SEQ_ID")
//EtlWorkflowSeq.id = PK of EtlWorkflowSeq entity
public EtlWorkflowSeq parent;
#OneToMany(fetch=FetchType.EAGER /*, mappedBy="parent"*/)
#JoinColumn(name="WORKFLOW_SEQ_ID")
public Set<EtlWorkflowSeq> children;
}
}
And I gen an error:
Caused by: org.hibernate.AnnotationException: A Foreign key refering models.EtlWorkflowSeqNode from models.EtlWorkflowSeq has the wrong number of column.
should be 2
Here is EtlWorkflowSeq entity:
#Entity
#Table(name="WORKFLOW_SEQ")
public class EtlWorkflowSeq {
#Id
#Column(name="WORKFLOW_SEQ_ID")
public Integer id;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name="WORKFLOW_ID")
public EtlWorkflow etlWorkflow;
}
What do I do wrong?
UPD:
Here are table definitions:
--a bad design. PK should be: WORKFLOW_SEQ_ID + REL_WORKFLOW_SEQ_ID + JOB_ID
CREATE MULTISET TABLE WORKFLOW_SEQ_REL ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
WORKFLOW_SEQ_ID INTEGER NOT NULL, --a ref to child
REL_WORKFLOW_SEQ_ID INTEGER NOT NULL, -- a ref to parent
START_TYPE_ID SMALLINT NOT NULL, -- a type of connection
DISABLE_START_TYPE_ID SMALLINT, -- other type of connection
JOB_ID BIGINT) -- a tree determinant,
PRIMARY INDEX ( WORKFLOW_SEQ_ID );
CREATE MULTISET TABLE WORKFLOW_SEQ ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
WORKFLOW_SEQ_ID INTEGER NOT NULL, -- an id
WORKFLOW_ID BIGINT NOT NULL, -- a ref to original workflow, not interesting
IS_NAME VARCHAR(255) CHARACTER SET UNICODE NOT CASESPECIFIC, -- some name
INFO_SYSTEM_INST_CD VARCHAR(255) CHARACTER SET UNICODE NOT CASESPECIFIC, -- other name
DISABLE BYTEINT) -- so garbage
UNIQUE PRIMARY INDEX ( WORKFLOW_SEQ_ID ); -- it should also be a PK
The Idea is that several trees are stored in WORKFLOW_SEQ_REL
JOB_ID is a determinant for trees.
WORKFLOW_SEQ_ID, REL_WORKFLOW_SEQ_ID refer some cutomized template from REL_WORKFLOW_SEQ table.
I cannot help noticing that there is an inconsistency in your question.
You first state that:
Leafs of these trees can have several children/parents.
This in my believe makes the relationship between leafs many to many.
As I make of your question that EtlWorkflowSeq represent leafs, I think EtlWorkflowSeqNode represents the relationship between EtlWorkflowSeq objects?
However, the nodes point to one parent and many children.
You can use something like this to create something similar:
#Entity
#Table(name="WORKFLOW_SEQ")
public class EtlWorkflowSeq
{
#Id
#GeneratedValue
#Column(name="WORKFLOW_SEQ_ID")
public Integer id;
#ManyToOne
#JoinColumn(name="WORKFLOW_ID")
public EtlWorkflow etlWorkflow;
#ManyToMany
#JoinTable(name = "WORKFLOW_SEQ_REL")
private Set<EtlWorkflowSeq> children;
#ManyToMany(mappedBy = "children")
private Set<EtlWorkflowSeq> parents;
#ManyToOne
#JoinColumn(name = "JOB_ID", referencedColumnName = "id")
private Job job;
}
This would make EtlWorkflowSeqNode and EtlWorkflowSeqNodeId obsolete.
I also would like to state that when using an #Embeddable you should only use base types in them. Using other than base types is not possible/causes problems/is not standard (correct me if I'm wrong).
If you would like to use foreign keys in a composite primary key you can use this:
#Entity
public class Foo
{
#Id
private Long id;
}
#Entity
public class Bar
{
#EmbeddedId
private BarPK key;
#MapsId(value = "fooId")
#ManyToOne
#JoinColumns({
#JoinColumn(name = "foo_id", referencedColumnName = "id")
})
private Foo foo;
}
#Embeddable
public class BarPK
{
#Column(name = "id")
private Long id;
#Column(name = "foo_id")
private Long fooId;
}

one-to-one relationship using JPA

I have created two table using JPA. i need to give 1-1 relationship between these tables. Can any one tell me how to give relationship between these tables.
Simply add a column in the table "owning" the relation with a FK constraint. For example:
CREATE TABLE MYENTITYA (
ID BIGINT NOT NULL,
MYENTITYB_ID BIGINT
);
CREATE TABLE MYENTITYB (
ID BIGINT NOT NULL
);
ALTER TABLE MYENTITYA ADD CONSTRAINT SQL100326144838300 PRIMARY KEY (ID);
ALTER TABLE MYENTITYB ADD CONSTRAINT SQL100326144838430 PRIMARY KEY (ID);
ALTER TABLE MYENTITYA ADD CONSTRAINT FKB65AC952578E2EA3 FOREIGN KEY (MYENTITYB_ID)
REFERENCES MYENTITYB (ID);
That would be mapped like this:
#Entity
public class MyEntityA implements Serializable {
private Long id;
private MyEntityB myEntityB;
#Id
#GeneratedValue
public Long getId() {
return this.id;
}
#OneToOne(optional = true, cascade = CascadeType.ALL)
public MyEntityB getEntityB() {
return this.myEntityB;
}
//...
}
#Entity
public class MyEntityB implements Serializable {
private Long id;
#Id
#GeneratedValue
public Long getId() {
return id;
}
//...
}
If the relation between EntityA and EntityB is not optional, then add a NOT NULL constraint.

Persisting a List of Integers with JPA?

We have a pojo that needs to have a list of integers. As an example, I've created a Message pojo and would like to associate a list of groupIds (these ids need to be queried and displayed in the UI). So ideally, we would like to be able to do something like this:
Message msg = em.find(Message.class, 101);
List<Integer> groupIds = msg.getGroupIds();
I was under the impression that this would require only one pojo with JPA, but according to the discussion here, I need to create a second pojo because JPA works in terms of objects instead of primitive types.
From that discussion I've tried the following example code, but I get the error openjpa-1.2.3-SNAPSHOT-r422266:907835 fatal user error: org.apache.openjpa.util.MetaDataException: The type of field "pojo.Group.messageId" isn't supported by declared persistence strategy "ManyToOne". Please choose a different strategy.
DDL:
CREATE TABLE "APP"."MESSAGE" (
"MESSAGE_ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
"AUTHOR" CHAR(20) NOT NULL
);
ALTER TABLE "APP"."MESSAGE" ADD CONSTRAINT "MESSAGE_PK" PRIMARY KEY ("MESSAGE_ID");
CREATE TABLE "APP"."GROUP_ASSOC" (
"GROUP_ID" INTEGER NOT NULL,
"MESSAGE_ID" INTEGER NOT NULL
);
ALTER TABLE "APP"."GROUP_ASSOC" ADD CONSTRAINT "GROUP_ASSOC_PK" PRIMARY KEY ("MESSAGE_ID", "GROUP_ID");
ALTER TABLE "APP"."GROUP_ASSOC" ADD CONSTRAINT "GROUP_ASSOC_FK" FOREIGN KEY ("MESSAGE_ID")
REFERENCES "APP"."MESSAGE" ("MESSAGE_ID");
POJOs:
#Entity
#Table(name = "MESSAGE")
public class Message {
#Id
#Column(name = "MESSAGE_ID")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long messageId;
#OneToMany
private List<Group> groups = new ArrayList<Group>();
#Column(name = "AUTHOR")
private String author;
// getters/setters ommitted
}
#Entity
#IdClass(pojo.Group.GroupKey.class)
#Table(name = "GROUP_ASSOC")
public class Group {
#Id
#Column(name = "GROUP_ID")
private Long groupId;
#Id
#Column(name = "MESSAGE_ID")
#ManyToOne
private Long messageId;
public static class GroupKey {
public Long groupId;
public Long messageId;
public boolean equals(Object obj) {
if(obj == this) return true;
if(!(obj instanceof Group)) return false;
Group g = (Group) obj;
return g.getGroupId() == groupId && g.getMessageId() == messageId;
}
public int hashCode() {
return ((groupId == null) ? 0 : groupId.hashCode())
^ ((messageId == null) ? 0 : messageId.hashCode());
}
}
// getters/setters ommitted
}
Test Code:
EntityManager em = Persistence.createEntityManagerFactory("JPATest").createEntityManager();
em.getTransaction().begin();
Message msg = new Message();
msg.setAuthor("Paul");
em.persist(msg);
List<Group> groups = new ArrayList<Group>();
Group g1 = new Group();
g1.setMessageId(msg.getMessageId());
Group g2 = new Group();
g2.setMessageId(msg.getMessageId());
msg.setGroups(groups);
em.getTransaction().commit();
This all seems ridiculous -- 3 classes (if you include the GroupKey composite identity class) to model a list of integers -- isn't there a more elegant solution?
This is an old topic but things have changed since OpenJPA2, now you can directly persist primitive types or String object. Use ElementCollection annotation to use simple one-to-many linking, no need to intermediate object or link tables. This is how most of us probably create SQL schemas.
#Entity #Table(name="user") #Access(AccessType.FIELD)
public class User {
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
private long id; // primary key (autogen surrogate)
private String name;
// ElementCollection provides simple OneToMany linking.
// joinColumn.name=foreign key in child table. Column.name=value in child table
#ElementCollection(fetch=FetchType.LAZY)
#CollectionTable(name="user_role", joinColumns={#JoinColumn(name="user_id")})
#Column(name="role")
private List<String> roles;
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name=name; }
public List<String> getRoles() { return roles; }
public void setRoles(List<String> roles) { this.roles=roles; }
}
- - -
CREATE TABLE user (
id bigint NOT NULL auto_increment,
name varchar(64) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY USERNAME (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
CREATE TABLE user_role (
user_id bigint NOT NULL,
role varchar(64) NOT NULL default '',
PRIMARY KEY (user_id, role)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
I really think that what you have is in fact a many-to-many association between two Entities (let's call them Message and Group).
The DDL to represent this would be:
CREATE TABLE "APP"."MESSAGE" (
"MESSAGE_ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
"AUTHOR" CHAR(20) NOT NULL
);
ALTER TABLE "APP"."MESSAGE" ADD CONSTRAINT "MESSAGE_PK" PRIMARY KEY ("MESSAGE_ID");
CREATE TABLE "APP"."GROUP" (
"GROUP_ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)
);
ALTER TABLE "APP"."GROUP" ADD CONSTRAINT "GROUP_PK" PRIMARY KEY ("GROUP_ID");
CREATE TABLE "APP"."MESSAGE_GROUP" (
"GROUP_ID" INTEGER NOT NULL,
"MESSAGE_ID" INTEGER NOT NULL
);
ALTER TABLE "APP"."MESSAGE_GROUP" ADD CONSTRAINT "MESSAGE_GROUP_PK" PRIMARY KEY ("MESSAGE_ID", "GROUP_ID");
ALTER TABLE "APP"."MESSAGE_GROUP" ADD CONSTRAINT "MESSAGE_GROUP_FK1" FOREIGN KEY ("MESSAGE_ID")
REFERENCES "APP"."MESSAGE" ("MESSAGE_ID");
ALTER TABLE "APP"."MESSAGE_GROUP" ADD CONSTRAINT "MESSAGE_GROUP_FK2" FOREIGN KEY ("GROUP_ID")
REFERENCES "APP"."MESSAGE" ("GROUP_ID");
And the annotated classes:
#Entity
public class Message {
#Id
#Column(name = "MESSAGE_ID")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long messageId;
#ManyToMany
#JoinTable(
name = "MESSAGE_GROUP",
joinColumns = #JoinColumn(name = "MESSAGE_ID"),
inverseJoinColumns = #JoinColumn(name = "GROUP_ID")
)
private List<Group> groups = new ArrayList<Group>();
private String author;
//...
}
#Entity
public class Group {
#Id
#GeneratedValue
#Column(name = "GROUP_ID")
private Long groupId;
#ManyToMany(mappedBy = "groups")
private List<Message> messages = new ArrayList<Message>();
//...
}
I'm not sure you need a bi-directional association though. But you definitely need to start to think object if you want to use JPA (in you're example, you're still setting ids, you should set Entities). Or maybe JPA is not what you need.
isn't there a more elegant solution?
I'm not sure "elegant" is appropriate but JPA 2.0 defines an ElementCollection mapping (as I said in my previous answer):
It is meant to handle several non-standard relationship mappings. An ElementCollection can be used to define a one-to-many relationship to an Embeddable object, or a Basic value (such as a collection of Strings).
But that's in JPA 2.0. In JPA 1.0, you would have to use a provider specific equivalent, if your provider does offer such an extension. It appears that OpenJPA does with #PersistentCollection.
Based on your schema you have a ManyToOne relationship between Group and Message. Which means that a single Message can belong to multiple groups, but each group can have a single message.
The entities would look something like this.
#Entity
#Table(name = "GROUP_ASSOC")
public class Group {
#Id
#Column(name="GROUP_ID")
private int id;
#ManyToOne
#Column(name="MESSAGE_ID")
#ForeignKey
private Message message;
// . . .
}
#Entity
public class Message {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "MESSAGE_ID")
private int id;
#Column(length=20)
private String author;
#OneToMany(mappedBy="message")
private Collection<Group> groups;
}
There's no need for an IDClass in your app (you only need one if your ID is contains multiple columns).
To get the groupIds for a given message you could write a query like this one
Query q = em.createQuery("Select g.id from Group g where g.message.id = :messageId");
q.setParameter("messageId", 1);
List results = q.getResultList();
Or just iterate over Message.getGroups() :
Message m = em.find(Message.class, 1);
for(Group g : m.getGroups()) {
// create a list, process the group whatever fits.
}