f.conversano
(Francesco Conversano)
June 23, 2016, 8:42am
#1
Hi,
Can i create a custom superclass for all entities?
Example
Before
public class Customer extends StandardEntity
After
Define superclass
public class MyStandarEntity extends StandardEntity
and
public class Customer extends MyStandarEntity
I have the following error:
java.lang.IllegalStateException: Entity com.company.sales.entity.Customer has no attribute applicable for table id column
stukalov
(Aleksey Stukalov)
June 23, 2016, 11:26am
#2
Hi Francesco,
Sure, you can define your own superclass. You should annotate it as
@MappedSuperclass
or just select MappedSuperclass checkbox in Enity editor in CUBA Studio.
According to the documentation , it ‘defines that the class is an ancestor for some entities and its attributes must be used as part of descendant entities. Such class is not associated with any particular database table ’.
See an example in the attachment.
Regards,
Aleksey
CustomSuperclass.zip (25.6K)
f.conversano
(Francesco Conversano)
June 23, 2016, 2:56pm
#3
I forgot to edit persistence.xml. Now works.
Thanks.
f.conversano
(Francesco Conversano)
June 23, 2016, 3:52pm
#4
Another question.
I would like set Field1 ( the field is not in DB ) during instantiation of MyStandardEntity object.
In my applications Spring based , I do this in the rowMapper .
How should I do it in Cuba?
thanks again
stukalov
(Aleksey Stukalov)
June 23, 2016, 4:54pm
#5
Francesco,
If you don’t want a property to be stored in the DB, add @Transient annotation to it:
@Transient
@MetaProperty
protected String field1;
Regarding property initialization ‘during instantiation ’, I’m not quite sure what you mean here.
If the question is about new instance creation you can simply add initialization logic to the constructor:
@MappedSuperclass
public class MyStandardEntity extends StandardEntity {
private static final long serialVersionUID = 7505674951206475460L;
@Transient
@MetaProperty
protected String field1;
...
public MyStandardEntity() {
this.field1 = UUID.randomUUID().toString();;
}
...
}
If you mean initialization on reading entity from DB, just create before detach entity listener :
@Component("customsuperclass_MyStandardEntityListener")
public class MyStandardEntityListener implements BeforeDetachEntityListener<MyStandardEntity> {
@Override
public void onBeforeDetach(MyStandardEntity entity, EntityManager entityManager) {
entity.setField1(UUID.randomUUID().toString());
}
}
Documentation on entity listeners can be found here .
Also, it would be helpful to have a look at the ‘Assigning Initial Values’ section of the documentation.
Regards,
Aleksey