How to persist "Set<String>" with Java Persistence API ?

Hello, I am using the EBJ 3.0 specs with JEE5.

I want to persist an @Entity that contains a field "Set<String> phoneNumbers;" but I get errors during the verification of the program by the Application Server of Sun and also by the verifier tool in netbeans 5.5.

I saw that if I make the String become a "@Entity PhoneNumber", then I can persist it without any problem.

My question is : Do I need to create an Entity for each type of field that have a relation @OneToMany or @ManyToMany with my current @Entities, even if they are basic types (e.g. List<Integer>, Set<String>, Collection<Boolean>, etc ...) ? If there is any trick to avoid to explicitly make a different class with an entity, I would be really happy to hear how.

[777 byte] By [karmaGfaa] at [2007-10-2 23:44:07]
# 1
Try @LobThat will persist the set as a BLOB
DrS@STVConsultantsa at 2007-7-14 16:28:09 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Following your advice, I tried but it doesn't work, I get an exception, always the same :

java.lang.StringIndexOutOfBoundsException: String index out of range: 3

at java.lang.String.substring(String.java:1765)

at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataHelper.getAttributeNameFromMethodName(MetadataHelper.java:99)

at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor.getAccessorFor(MetadataDescriptor.java:317)

at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor.processMapKey(MetadataProcessor.java:1295)

at oracle.toplink.essentials.internal.ejb.cmp3.annotations.EJBAnnotationsProcessor.processMapKey(EJBAnnotationsProcessor.java:1327)

at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor.populateCollectionMapping(MetadataProcessor.java:467)

at oracle.toplink.essentials.internal.ejb.cmp3.annotations.EJBAnnotationsProcessor.populateCollectionMapping(EJBAnnotationsProcessor.java:484)

at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor.processOneToMany(MetadataProcessor.java:1333)

at oracle.toplink.essentials.internal.ejb.cmp3.annotations.EJBAnnotationsProcessor.processOneToMany(EJBAnnotationsProcessor.java:1487)

at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor.processRelationshipAccessor(MetadataProcessor.java:1643)

at oracle.toplink.essentials.internal.ejb.cmp3.annotations.EJBAnnotationsProcessor.processRelatedEntity(EJBAnnotationsProcessor.java:1786)

at oracle.toplink.essentials.internal.ejb.cmp3.annotations.EJBAnnotationsProcessor.processORAnnotations(EJBAnnotationsProcessor.java:1543)

at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:405)

at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createContainerEntityManagerFactory(EntityManagerFactoryProvider.java:156)

at com.sun.enterprise.tools.verifier.tests.persistence.DefaultProviderVerification.check(DefaultProviderVerification.java:80)

at com.sun.enterprise.tools.verifier.CheckMgr.check(CheckMgr.java:120)

at com.sun.enterprise.tools.verifier.persistence.PersistenceUnitCheckMgrImpl.check(PersistenceUnitCheckMgrImpl.java:83)

at com.sun.enterprise.tools.verifier.CheckMgr.checkPersistenceUnits(CheckMgr.java:377)

at com.sun.enterprise.tools.verifier.ejb.EjbCheckMgrImpl.check(EjbCheckMgrImpl.java:67)

at com.sun.enterprise.tools.verifier.BaseVerifier.verify(BaseVerifier.java:133)

at com.sun.enterprise.tools.verifier.ejb.EjbVerifier.verify(EjbVerifier.java:65)

at com.sun.enterprise.tools.verifier.VerificationHandler.runVerifier(VerificationHandler.java:223)

at com.sun.enterprise.tools.verifier.VerificationHandler.verifyArchive(VerificationHandler.java:128)

at com.sun.enterprise.tools.verifier.Verifier.verify(Verifier.java:131)

at com.sun.enterprise.tools.verifier.Verifier.main(Verifier.java:101)

Is it my program that is not right or is it a bug of the persistence CMP ?

karmaGfaa at 2007-7-14 16:28:09 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

That depends how you expect Set<String> become stored:

1. Another Table with relationship.

You must write other Entity.

2. Varchar: value1, value2.

Create another getter:

@Column

public String getValue() {

String str;

// construct string from Set<String>

return str;

}

Methods related with Set<String> become @Transient.

I hope this help you.

Message was edited by:

Rulas

Rulasa at 2007-7-14 16:28:09 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Your answer helped me. Thank you.
karmaGfaa at 2007-7-14 16:28:09 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Hi guys!I've been struggling with this similar type of problem for a while now... Still didn't get it working. Could you please paste some example code since you got it working?Thanks,Samuli
S.Pielaa at 2007-7-14 16:28:09 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...