Can I put annotations for multiple processors in a Java class?

I'm not quite sure how annotations work. I use JAXB and JPA (with eclipselink) in the same classes, i.e. e. I get class definitions as follows:

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Channel") @Entity public class Channel extends NamedEntity { @XmlAttribute @XmlSchemaType(name = "anyURI") @Column(name="url") protected String url; @XmlAttribute @Column protected String coDirection; // etc... } 

Now I get strange errors (for example, "com.econemon.suite.common.models.Channel@115c974 is not a known entity type", which usually means that the class is not in my persistence.xml) and I wonder if there will be annotations interfere with each other?

+1
java annotations jpa jaxb
Jun 23 '09 at 17:57
source share
2 answers

Annotations cannot interfere with another annotation, especially. not if they come from different packages and have different names. Each structure (JAXB and Hibernate) has its own annotations. Each framework has only its own annotations. I am very sure that this is not a problem.

Please check your face for availability:

a) no argument constructor (the default constructor is good, but if you have only constructors with arguments, you must provide an argument constructor for JPA, this can be protected or visible in the package).

b) @Id Annotation for a field or getter (we always use field-based annotations, otherwise we will never mix JPA-getter annotation with field annotation in type!)

c) the type is in your persistence system and the unit of measure for duration is used

d) the type has @Entity Annotation

if you are using extends NamedEntity, you should provide more JPA with more information on how to map this to you. First try removing the extended NamedEntity stuff. And look at a good JPA tutorial that explains inheritance options.

+3
Jun 24 '09 at 21:08
source share

No, they do not.

I removed all other annotations and reduced inheritance and turned out to be a very simple class. Problem still exists.

What was not mentioned in my post (because I thought it didn't matter) was that I run it inside the OSGi container (if that matters, Felix). Now such a container protects different "bundles" from each other, so they cannot see each other's classes until you specifically "export" packages.

The annotated classes were in a different package than my persistence.xml, and I suggested that I could just import the annotated classes from another package and execute persistence initialization material elsewhere. It turns out I can’t, although I don’t understand why.

So, if you use JPA along with OSGi packages, you must make sure that:

  • Annotated classes and persistence.xml are together in one set
  • this package exports a package containing annotated classes
  • stability units are specified in the package manifest file

Then you can perform the actual persistence actions (for example, calling EntityManager.persist) in different packages.

As a side note, I got similar weird errors when trying to use JAXB annotations for different packages. It seems that JAXBContext and / or ObjectFactory should be created in the same package that contains the annotated classes. I could not do this, but investing in the same bunch helped.

It would be great if someone with a deeper understanding of OSGi, class loading and annotations could comment on what could happen here.

Update : exporting / importing appropriate annotation packages may allow you to have persistence.xml and annotated classes in different packages, see. However, not tested.

0
Jun 23 '09 at 18:12
source share



All Articles