Datanucleus enhancer error: no objectid-class - java

I have the below domain model class
#Entity #Access(AccessType.FIELD)
public class MyBean {
#Id #GeneratedValue
private Long id;
...
#Transient
public String getOther() {
...
}
}
Running DataNucleus Enhancer gives the following error:
Class MyBean has application-identity and no objectid-class specified yet has 0 primary key fields. Unable to use SingleFieldIdentity.
Why?

It seems that the problem is the #Transient annotation on the method. Removing it solved the issue.
Still, in this case, taking into account the #Access and #Id annotation on a field, the #Transient annotation should be ignored. Or, preferably, the error message should be improved.

Related

Embeddable not enhanced with OpenJpa

SOLVED
Metadata classes and Entity/Embeddable classes have different names. I just renamed them and now it fires up
I'm trying to map an entity on my DB with my jpa web application. The problem is that the entity got a 2 elements key and, also if i'm using an embeddable, after launching my app with tomcat, it displays this error:
Caused by: <openjpa-2.4.0-r422266:1674604 fatal user error> org.apache.openjpa.util.MetaDataException: The type "class it.cabel.aml.libb2b.cliente.jpa.entities.JpaKeyPK" has not been enhanced.
I'm changing the name of classes just for readability.
This is the JpaKeyPK class:
//import...
#Embeddable
public class JpaKeyPK implements Serializable {
#Column(name = "ID")
private Integer id;
#Column(name = "DATA_VARIAZIONE")
private Date dataVariazione;
public JpaKeyPK () {
}
public JpaKeyPK (Integer id, Date dataVariazione) {
this.id = id;
this.dataVariazione = dataVariazione;
}
// getters, setters, hashcode and equals implementation...
And this is the entity class:
#Entity
#Table(name = "TABLE_NAME")
public class JpaEntity{
#EmbeddedId
private JpaKeyPK pk;
//getters, setters, constructor...
I added both of them to my persistence.xml file and, when i try to do a clean install with maven, logs write this:
12670 App INFO [main] openjpa.Tool - Enhancer running on type "class it.jpa.entities.JpaEntity".
12671 App INFO [main] openjpa.Tool - Enhancer running on type "class it.jpa.entities.JpaKeyPK".
What's wrong?

Javers with Spring Boot returning ENTITY_INSTANCE_WITH_NULL_ID for a #Transient field

I've just started using Javers on my Application but I have entities annoted with #Transient that I thought Javers would ignore than, but no :(, instead it's throwing me an exception:
JaversException ENTITY_INSTANCE_WITH_NULL_ID: Found Entity instance 'ProductData' with null Id-property 'id'
Do you guy know if there is a way to Ignore those transient fields?
The Documentation says that the #Transient annotation is a synonym for #DiffIgnore. But i dont know if that is related to only comparacion, or during the audit flow as well.
Here is my code:
#Entity
public class ProductExternal extends AbstractEntity implements ExternalEntity {
#Transient
private ProductData productData;
#NotNull
#Column(unique=true)
private Long externalId;
public ProductExternal() { }
//get set
}
--
#Entity
public class ProductData extends AbstractEntity {
private static final long serialVersionUID = 1L;
#Column
#NotNull
private String name;
public ProductData() { }
//get set
}
Parent class
#MappedSuperclass
public abstract class AbstractEntity implements Serializable {
public AbstractEntity() {}
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
#Version
#Column(columnDefinition = "bigint default '0'")
protected Long version;
//get set
}
Your class and mapping (annotations) seems fine. The exception is saying:
Found Entity instance 'ProductData' with null Id-property 'id'
So you are trying to commit to Javers an object of class ProductData which has null id field. Obviously that's not possible. That's a common issue with Hibernate's #GeneratedValue magic. Your field is null at the first place, and then it's being updated later by Hibernate after calling DB sequence next val.
Generally, you should call Javers commit() after Hibernate is done with persisting your object. It can be easily achieved when using one of Javers' auto-audit aspects: #JaversAuditable or #JaversSpringDataAuditable. They are applied in the right phase and call Javers commit() for you. See https://javers.org/documentation/spring-integration/#auto-audit-aspect.

How to fix validation constraints in embedded object beeing ignored?

I have my entity object in Spring Boot & Hibernate REST API. This class has many fields. Part of them is embedded and validation constraints such as #Min #Max are not working on fields in #Embeddable class. Same validation rules work perfect in #Entity classes. I am using javax.validation.constraints.Max
My main object looks like this:
#Entity
public class Notice extends BaseEntity {
#Embedded
private MyEmbeddedClass myEmbeddedClass;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "entity_class_id")
private MyEntityClass myEntityClass;
}
And my #embedded class:
#Embeddable
public class MyEmbeddedClass {
#Size(max = 50)
private String label;
#Max(100)
private Integer percent;
}
#Max constraint on percent field is ignored, but #size is working perfectly
#Entity
public class MyEntityClass extends BaseEntity {
#Size(max = 50)
private String name;
#Max(6000)
private Integer size;
}
And here #Max constraint and #size constraint on fields size are beeeing created
Is there a way to fix this? My Spring boot version is 2.1.1 and I can create my database scripts manually but I'd like to avoid that and get almost perfect script thanks to hibernate
You need to add #Valid annotation on your embedded object if you want to validate the constraints defined in your #Embeddable object:
#Entity
public class Notice extends BaseEntity {
#Embedded
#Valid
private MyEmbeddedClass myEmbeddedClass;
...
}

Can't put #entity annotation in auditable class

This is my class:
#Data
#Entity
#Table(name="usercontext")
public class UserContext extends AbstractAuditable<UserDO, Long> {
#Column(name="name") private String name;
#Column(name="value") private String value;
...
}
I am using lombok (hence #Data annotation). Eclipse editor complains that "The entity has no primary key attribute defined" although the code compiles properly and runs. This same error is shown in JPA problems in markers list in eclipse. AbstractAuditable extends AbstractPersistable which already has a primary key called "id" defined. Is there any way to remove these errors?
AbstractAuditable needs to be annotated with #MappedSuperclass in order for id to be visible to subclasses, and that id should be annotated with #Id.

#OneToOne annotation within composite key class is not working

Maybe somebody can clarify what is wrong with the code below. When I create one-to-one association within embedded class (it is composite primary key) like in the code below:
#Entity
public class Test {
#EmbeddedId
private TestId id;
#Embeddable
public static class TestId implements Serializable {
private static final long serialVersionUID = 1950072763330622759L;
#OneToOne(optional = false)
#JoinColumn(name = "linkedTable_id")
private LinkedTable linkedTable;
}
..........
}
I get the following stack trace:
--------------------------------------------
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.AnnotationBinder.bindOneToOne(AnnotationBinder.java:1867)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1286)
at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:1662)
at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1695)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1171)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:706)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:452)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:268)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1211)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:847)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:178)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:235)
... 26 more
What is interesting why the sample above works if I change association type to many-to-one and doesn't work with one-to-one?
I wasn't aware this was possible but, according to the Hibernate Annotation reference documentation, it is (this is Hibernate specific though):
2.2.3.2.1. #EmbeddedId property
(...)
While not supported in JPA, Hibernate
lets you place your association
directly in the embedded id component
(instead of having to use the
#MapsId annotation).
#Entity
class Customer {
#EmbeddedId CustomerId id;
boolean preferredCustomer;
}
#Embeddable
class CustomerId implements Serializable {
#OneToOne
#JoinColumns({
#JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
#JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
})
User user;
String customerNumber;
}
#Entity
class User {
#EmbeddedId UserId id;
Integer age;
}
#Embeddable
class UserId implements Serializable {
String firstName;
String lastName;
}
And with the code you provided, the following snippet just works for me:
LinkedTable linkedTable = new LinkedTable();
linkedTable.setId(1l);
session.persist(linkedTable);
session.flush();
Test.TestId testId = new Test.TestId();
testId.setLinkedTable(linkedTable);
Test test = new Test();
test.setId(testId);
session.persist(test);
session.flush();
Tested with Hibernate EM 3.4.0.GA, Hibernate Annotations 3.4.0.GA and Hibernate Core 3.3.0.SP1.
If it doesn't work for you, can you provide a bit more code allowing to reproduce the problem?

Categories

Resources