How to make a request factory non-transportable type transportable? - gwt

According to this answer: Is com.vividsolutions.jts.geom.Geometry directly transportable using requestfactory? Geometry is ( a particular case of a type that is ) non- transportable using requestfactory.
So then would this work? :
#Entity
public class Poi {
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Id
private Integer id;
#Type(type="org.hibernate.spatial.GeometryType")
private Geometry geom;
//bi-directional many-to-one association to PoiCateg
#ManyToOne
#JoinColumn(name="id_cat")
private PoiCateg poiCateg;
#Version
private Integer version;
public Poi() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Geometry getGeom() {
return this.geom;
}
public void setGeom(Geometry geom) {
this.geom = geom;
}
public PoiCateg getPoiCateg() {
return this.poiCateg;
}
public void setPoiCateg(PoiCateg poiCateg) {
this.poiCateg = poiCateg;
}
//not your standard getters and setters
public String getGeomClient() {
return //result of method that converts from Geometry object to WKT string representation
}
public void setGeomClient(String geom) {
this.geom = // result of method that converts from String to Geometry
}
}
and then my modified entity proxy for Poi would look like:
#ProxyFor(value=Poi.class)
public interface PoiProxy implements EntityProxy {
public Integer getId() ;
public void setId(Integer id);
public PoiCategEntityProxy getPoiCateg() ;
public void setPoiCateg(PoiCateg poiCateg);
//not your standard getters and setters
public String getGeomClient() ;
public void setGeomClient(String geom) ;
}
since getGeomClient and setGeomClient in the server entity contain a geometry type will it be a problem on the client?
EDIT1: forgot about #Version private Integer version; mistake fixed.

Not only it'll work but that's the (simplest) way to make it work.
Alternatives involve using wrappers/builders. I've also seen people using EntityProxys where the stringified value is used as the identifier, but beware that RequestFactory requires a per-request cache.

Related

get max value of column using jpa criteria query

I want to get the maximum value of column relationId from table ElementRelationType
I have written code but its giving error
CriteriaBuilder cb1 = entityManager.getCriteriaBuilder();
CriteriaQuery<ElementRelationTypes> cq1 = cb1.createQuery(ElementRelationTypes.class);
Root<ElementRelationTypes> root = cq1.from(ElementRelationTypes.class);
cq1.select(cb1.max(root.get("relationId")));
select and max both giving error
how to get the integer max value
public class ElementRelationTypes {
private RelationId relationLangPK=new RelationId();
private Country country;
private Status status;
#EmbeddedId
public RelationId getRelationLangPK() {
return relationLangPK;
}
public void setRelationLangPK(RelationId relationLangPK) {
this.relationLangPK = relationLangPK;
}
#Transient
public Integer getRelationId() {
return getRelationLangPK().getRelationId();
}
public void setRelationId(Integer relationId) {
getRelationLangPK().setRelationId(relationId);
}
#Transient
public Language getLanguage() {
return getRelationLangPK().getLanguage();
}
public void setLanguageCode(Language language) {
getRelationLangPK().setLanguage(language);
}
and
public class RelationId implements Serializable {
private static final long serialVersionUID = 1L;
private Integer relationId;
private Language language;
#JoinColumn(name=PersistenseConstants.ELEMENT_RELATION_TYPE_COL_RELATION_ID)
public Integer getRelationId() {
return relationId;
}
public void setRelationId(Integer relationId) {
this.relationId = relationId;
}
#OneToOne
#JoinColumn(name=PersistenseConstants.LANGUAGE_ENTITY_COL_LANG_CODE)
public Language getLanguage() {
return language;
}
public void setLanguage(Language language) {
this.language = language;
}
You didn't post which errors do you receive, so I have to guess.
CriteriaBuilder.max accepts Expression<N> where N extends Number
At the same time Root.get by default returns Path<Object> which is inconvertible to Expression<Number>.
So to make your call to max work you need to specify generic parameter to root.get:
cq1.select(cb1.max(root.<Number>get("relationId")));
here you can replace Number with an actual type of relationId (Long, BigInteger etc.)
UPDATE: #perissf addressed another issue with your code. If you are going to select maximal value (which is numeric) you should declare your CriteriaQuery as a query to Number not ElementRelationTypes

JPA EmbeddedId is null after persisting

Let's say I have 2 JPA entites
#Entity
public class MyComplexEntity implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
#GeneratedValue(strategy = GenerationType.IDENTITY)
private MyComplexEntityId id;
private String text;
public MyComplexEntity() {
}
public MyComplexEntity(String text) {
this.text = text;
}
public MyComplexEntityId getId() {
return id;
}
public void setId(MyComplexEntityId id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
#Override
public String toString() {
return "MyComplexEntity{" + "id=" + id + ", text=" + text + '}';
}
}
and
#Embeddable
public class MyComplexEntityId {
#Column
private long id;
public MyComplexEntityId(long id) {
this.id = id;
}
public MyComplexEntityId() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
#Override
public String toString() {
return "MyComplexEntityId{" + "id=" + id + '}';
}
}
Now I'm trying to persist a new MyComplexEntity object like this
MyComplexEntity entity = new MyComplexEntity("something");
em.persist(entity);
System.out.println(entity);
and what I get is this:
MyComplexEntity{id=null, text=something}
Why is the id null there? If I do the same thing with a primitive primary key type, the id is correctly set after persisting. I've also tried the following things:
Calling em.flush(); after the persist: does nothing
Calling em.refresh(entity); after the persist: Exception telling me that the entity is no longer in the database (which is wrong)
Trying em.merge() instead of em.persist(): Same result, as expected
Calling em.contains(entity); returns true, so the entity is actually attached
Putting the em.persist(); in a separate transaction by executing it in a separate method using #TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
There has to be some kind of problem with persist(); when using an #EmbeddedId, but I just don't get it.
You cannot put GeneratedValue(strategy = GenerationType.IDENTITY) on a complex mapping. It needs to go within the embeddedId class, and you need to instantiate a MyComplexEntityId instance for your new Entity. You will also need to flush to the database to ensure that values are assigned as identity values are assigned by the insert.

Google Objectify v4 fetch by id

I'm trying to fetch an entity in App Engine with Objectify v4 but it doesn't work.
My #Entity: Translation.class
The #Id of the #Entity I want to fetch: 301L
My #Entity:
#Entity
public class Translation {
#Id
private Long id;
private String text;
public String getText() {
return text;
}
public Long getId() {
return id;
}
public void setText(String text) {
this.text = text;
}
}
The request that doesn't word:
Translation translation =ObjectifyService.ofy().load().type(Translation.class).id(301L).get(); // translation is null
But if I do:
Translation translation = ObjectifyService.ofy().load().type(Translation.class).first().get(); // translation is not null
Then:
System.out.println(translation.getId()); // translation id equal 301
So the fetch by id doesn't seem to work.
Where is the problem?
Since your entity has a #Parent field, in order to get it by id, you need to execute:
Translation translation = ObjectifyService.ofy().load().type(Thing.class).parent(par).id(301).get();
For more information take a look at Objectify Basic Operations - Loading
Hope this helps!
for #stickfigure, this is my real #Entity (PartOfSpeechGroup is obviously an #Entity too).
#Entity
public class Translation implements IsSerializable {
#Id
private Long id;
private String text;
#Parent
private Key<PartOfSpeechGroup> partOfSpeechEntryKey;
public Translation() {}
public Translation(Key<PartOfSpeechGroup> partOfSpeechEntryKey) {
this.partOfSpeechEntryKey = partOfSpeechEntryKey;
}
public String getText() {
return text;
}
public Long getId() {
return id;
}
public Key<PartOfSpeechGroup> getPartOfSpeechEntryKey() {
return partOfSpeechEntryKey;
}
public void setText(String text) {
this.text = text;
}
}

Loading child entities in parent/child relationship with JPA

My domain has a Category entity which has a biderectional relationship on itself. Each category can have a parent and children.
#Entity
public class Category implements DomainObject {
private Long id;
private Integer version;
private String name;
private Category parent;
private Set<Category> children;
#Override
#Id
#GeneratedValue
public final Long getId() {
return id;
}
#Version
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public void setId(Long id) {
this.id = id;
}
#Column(unique=true, nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#ManyToOne
public Category getParent() {
return parent;
}
public void setParent(Category parent) {
this.parent = parent;
}
#OneToMany
#JoinColumn(name = "parent_id")
public Set<Category> getChildren() {
return children;
}
public void setChildren(Set<Category> children) {
this.children = children;
}
}
I have created the following query to fetch the "root" categories with their direct (level 1) children.
select distinct c from Category c left join fetch c.children where c.parent is null order by c.name
This actually works. My question is: why do I need the "JoinColumn" annotation on getChildren() to make this work and why can't I just make a "foin fetch" query, without "distinct"? If I remove "distinct" I get a multiplication. For each child of a parent, the entire parent is copied in the result set.
Is there a better way to do this? It just feels... a bit crappy.
In JPA you need to set distinct when you join a OneToMany, otherwise it will return duplicates.
This is required.
The JPA spec requires this, but it is an odd default, but relates to what happens in database joins.

Copy Entity ID at persist time

I want to copy the entity's UUID, generated at run time to another field.
The entity id is generated via the code described bellow:
package eclipselink.example;
public class UUIDSequence extends Sequence implements SessionCustomizer {
public UUIDSequence() {
super();
}
public UUIDSequence(String name) {
super(name);
}
#Override
public Object getGeneratedValue(Accessor accessor,
AbstractSession writeSession, String seqName) {
return UUID.randomUUID().toString().toUpperCase();
}
...
public void customize(Session session) throws Exception {
UUIDSequence sequence = new UUIDSequence("system-uuid");
session.getLogin().addSequence(sequence);
}
}
Persitence.xml:
property name="eclipselink.session.customizer" value="eclipselink.example.UUIDSequence"
The entity:
public abstract class MyEntity{
private String id;
private String idCopy;
#Id
#Basic(optional = false)
#GeneratedValue(generator="system-uuid")
#XmlElement(name = "ID")
public String getId() {
return id;
}
}
How can I instruct JPA (Eclipse-link) to copy the UUID generated at runtime to idCopy field as well?
I'm not 100% sure this will work (I don't know if EclipseLink calls the setter or assigns the field directly), but give this a try:
public abstract class MyEntity{
private String id;
private String idCopy;
#Id
#Basic(optional = false)
#GeneratedValue(generator="system-uuid")
#XmlElement(name = "ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
this.idCopy = id;
// or
// this.setIdCopy(id);
}
}