is it possible to use Constraints in the google-app-engine? It seems not to work ...
http://www.datanucleus.org/products/accessplatform_1_1/jpa/orm/constr...
The properties codingSystem and code should be unique. Is there a
workaround?
#Entity
#Table(uniqueConstraints = {
#UniqueConstraint(columnNames = { "codingSystem", "code" }) })
public class ArticleCode {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key id;
private String codingSystem;
private String code;
Thanks,
Ralph
In a nutshell, no, they're not. The underlying datastore implementation doesn't support global transactions, so it's not practical to enforce arbitrary uniqueness constraints.
The workaround is to make the unique components part of the key name.
Thanks a lot, it works fine.
Here is my new code.
#Entity
public class ArticleCode {
#Id
private Key id;
#Column(name="codingSystem")
private String codingSystem;
#Column(name="code")
private String code;
public ArticleCode(Key parent, String codingSystem, String code) {
this.id = KeyFactory.createKey(parent, ArticleCode.class.getSimpleName(), codingSystem + code);
this.codingSystem = codingSystem;
this.code = code;
}
Related
Now, I have two tables.
1st Table (Object Table):
RefNo (PK) --> auto ascending
Type
Status
...
2nd Table (Object Detail Table):
RefNo (PK) --> FK reference from 1st Table
PolicyNo
DepNo
...
Entity for Object.
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long refnno;
#OneToOne(mappedBy = "obejctTwo")
private ObjectDetail objectDetail;
private String type;
private String status;
Entity for ObjectDetail
#Id
private long refnno;
#OneToOne
#JoinColumn(name = "refnno")
#MapsId
private Object object;
private String policyNo;
private String depNo;
How can I save the Object using jpaRepository for Object which include ObjectDetail inside Object JSON but without knowing the reffno(PK) which is auto generated by db.
{
"objectDetail": {
"policyNo": "12345678",
"depNo": "ABC"
},
"type": "new",
"status": "pending"
}
Would it not be possible to cascade the saving with the annotation #OneToOne(cascade = CascadeType.PERSIST) in the class Object?
I would then create the two objects with the available details, set the relationship and save the class Object.
object.setObjectDetail(objectDetail);
objectdetail.setObject(object);
repository.save(object);
The key of object would be set automatically and objectDetail would get the key from object.
Edit: Entity Example
I was slightly surprised that the code did not work. Therefore, I implemented this for myself. It works perfectly with Spring Boot version 2.3.2.RELEASE and spring-boot-starter-data-jpa.
Your issue might be #OneToOne(mappedBy = "obejctTwo") because this mapping does not exist.
Object
#Entity
public class Object {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long refnno;
#OneToOne(mappedBy = "object",
cascade = CascadeType.ALL,
orphanRemoval = true)
private ObjectDetail objectDetail;
private String type;
private String status;
public setObjectDetail(ObjectDetail objectDetail) {
this.objectDetail = objectDetail;
objectDetail.setObject = this;
}
// Getters and remaining setters...
}
I added a slightly modified setter for objectDetail which helps to keep the bidirectional OneToOne mapping synchronised.
BTW: I changed the datatype of refnno to the object Long. Classes are considered to be better for database entities because than you can test them properly for null. Furthermore, this id can then be used for a JpaRepository<Object, Long>.
ObjectDetail
#Entity
public class ObjectDetail {
#Id
private Long refnno;
#OneToOne
#JoinColumn(name = "refnno")
#MapsId
private Object object;
private String policyNo;
private String depNo;
// Getters and setters...
}
Repository and Execution
I created a simple repository.
public interface ObjectRepository extends JpaRepository<Object, Long> {
}
I then used the save(Object entity) method to persist a new Object with a new ObjectDetail.
Object object = new Object();
object.setType("new");
ObjectDetail objectDetail = new ObjectDetail();
objectDetail.setPolicyNo = "999";
object.setObjectDetail(objectDetail);
objectRepository.save(object);
Can someone help me how should I join those three tables using JPA?
I already did 2 of 3 entities but please let me know if are ok:
#Entity
public class Pacienti {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String nume;
private String prenume;
//setters & getters
}
#Entity
public class Chestionare {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#Id
#Column(name = "id_intrebare")
#GeneratedValue(strategy = GenerationType.AUTO)
private int idIntrebare;
private String intrebare;
//setters & getters
}
As I promise I come back after I'm generating entities automatically. Unfortunately now I have another problem.
Now I have the entity:
#Entity
#Table(name = "pacienti")
#NamedQuery(name = "Pacienti.findAll", query = "SELECT p FROM Pacienti p")
public class Pacienti implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(unique = true, nullable = false)
private int id;
#Column(nullable = false, length = 20)
private String nume;
#Column(nullable = false, length = 20)
private String prenume;
// bi-directional many-to-one association to Consultatii
#OneToMany(mappedBy = "pacienti")
private List<Consultatii> consultatiis;
// bi-directional many-to-one association to DetaliiPacient
#OneToMany(mappedBy = "pacienti")
private List<DetaliiPacient> detaliiPacients;
// bi-directional many-to-one association to Doctori
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "id_doctor", nullable = false)
private Doctori doctori;
// bi-directional many-to-one association to RaspunsChestionar
#OneToMany(mappedBy = "pacienti")
private List<RaspunsChestionar> raspunsChestionars;
public Pacienti() {
}
//setters and getters
}
But when I do :
Query queryResult = sessionFactory.getCurrentSession().createQuery("from Pacienti");
I'm getting:
Pacienti is not mapped [from Pacienti] Error.
Can someone tell me why? I also tried "pacienti is not mapped [from pacienti]" but same result
Thank you!
I would recommend you to use the jpa tools/plugins available with the IDEs which will auto generate these jpa entities for you using the database tables rather than manually creating these.
And they will take care of setting the relationship b/w different entities(db tables) in the auto generation process itself.
If you are Eclipse you can achieve this.
The problem is bcz there is no query with the name "from pacienti" in place of that pass the query name "Pacienti.findAll" in your createQuery method.
Plz let ne know once you try this, if you face any problem
I don't know why I can't persistent MAP in JPA in GAE
AnnualReport thatyear = .......
if (stud.getAnnualReport() == null){
Map<Integer,AnnualReport> temp = new HashMap<Integer,AnnualReport>();
temp.put(thatyear.getAttrKey(), thatyear);
stud.setAnnualReport(temp);
} else{
Map<Integer,AnnualReport> temp2 = stud.getAnnualReport();
temp2.put(thatyear.getAttrKey(), thatyear);
stud.setAnnualReport(temp2);
}
em.getTransaction().begin();
try {
em.persist(stud);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
}
Actually in http:// localhost :8888/_ah/admin/datastore I can see the thatyear has been persistent; However, I can never get them; or, stud.getAnnualReport() is always empty.
EntityManager em;
em = EMF.get().createEntityManager();
AnnualReport thatyear = stud.getAnnualReport().get(yearselected);
I really don't know what to do. Following is the relationship between Stud & AnnualReport
Stud
#Entity( name = "Stud")
public class Stud{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key studID;
private String lastName = new String();
private Map<Integer,AnnualReport>annualReport = new HashMap<Integer,AnnualReport>(20);
#OneToMany(mappedBy="stud",cascade = CascadeType.ALL)
#MapKey(name = "attrKey")
#Basic
public Map<Integer, AnnualReport> getAnnualReport() {
return annualReport;
}
AnnualReport
#Entity( name = "AnnualReport")
public class AnnualReport implements Serializable{
private static final long serialVersionUID = 3581307841164176872L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key annualReportID;
public int attrKey;
#ManyToOne
Stud stud;
private String attendSchoolNote;
I don't know what happens. Why I can't get those map information which are already persistent?
No idea why you don't get the expected result, but then you present no debug info. You can easily follow the persistence process using the log, telling you what is actually persisted into the GAE Entity objects. GAE has a (JDO) unit test at
http://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/google/appengine/datanucleus/jdo/JDOMapTest.java
which demonstrates correct behaviour (and since JDO/JPA is simply a wrapper over the persistence engine, no reason to think the same would not persist fine using JPA).
Edit : in fact I just added a test for JPA maps at http://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/google/appengine/datanucleus/jpa/JPAMapTest.java and works fine.
I have a Java model similar to:
public class Country {
#Id private String id;
private CurrencyId currencyId;
private List<Province> provinceList;
...
}
public class Province {
#Id private String id;
private Gobernor gobernorId;
private List<City> cityList;
...
}
public class City {
#Id private String id;
private String name;
...
}
I want to store that data using objectify. However, as Country data might change, I also want to store the date the Country data has been stored, so I think I should store an entity such as:
public class CountryListEntity {
#Id private String id;
private List<Country> countryList;
private Date storeDate;
}
Note I will only have one entity of kind CountryListEntity with the Id "root", if I can store it like that. I know very little about both how google apps stores data and how objectify works. I've tried many combinations of #Embedded, but I got many errors, i.e.
Cannot place array or collection properties inside #Embedded arrays or collections
Can anyone tell me how to define these classes? A snippet of the code needed to store and retrieve this "root" entity, would be highly appreciated!
#Embedded collections are transformed into a series of collection fields in the
low-level Entity. That's why one level embedding is all you can do.
If you are going to store/load all data at once and if your entities are as simple as the ones in your example you can put #Serialized annotation for your lists inside #Embedded lists.
You can find out more from this discussion.
The problem with this approach is that your low-level embeddings won't be able to be indexed.
public class CountryListEntity {
#Id private String id;
#Embedded
private List<Country> countryList;
private Date storeDate;
}
public class Country implements Serializable {
private String id;
private CurrencyId currencyId;
#Serialized
private List<Province> provinceList;
// ...
}
public class Province implements Serializable {
private String id;
private Gobernor gobernorId;
#Serialized
private List<City> cityList;
// ...
}
public class City implements Serializable {
private String id;
private String name;
// ...
}
I am not sure if I use the #Id in objectify the right way.
Right now I am using the eMail-Address as #Id field. The email field will be set on the server-side only (OAuthService.getCurrentUser.getEmail)
First question: Is this a good idea?
If I create for example an Item-class which has RegistrationTO as it's parent does it make sense to use the email-address as the #Id field in my Item-class or should Item-class have it's own, auto-generated, id and Key parent to specify the relation?
Objectify-Tutorial recommends to avoid #Parent - so, here I think it's not necessary either.
I am right?
Here my RegistrationTO:
public class RegistrationTO implements Serializable {
private static final long serialVersionUID = 1L;
#NotNull
#Size(min = 5, max = 20)
private String firstname;
#NotNull
#Size(min = 5, max = 20)
private String name;
#NotNull
#Size(min = 5, max = 20)
private String country;
#Id
#NotNull
#Size(min = 5, max = 20)
#Pattern(regexp = "\b[A-Z0-9._%-]+#[A-Z0-9.-]+\\.[A-Z]{2,4}\b")
private String email;
public RegistrationTO() {
}
public RegistrationTO(final String firstname, final String name, final String company) {
this.firstname = firstname;
this.name = name;
this.country = country;
email = "will be set on server (Oauth)";
}
public String getFirstname() {
return firstname;
}
public String getName() {
return name;
}
public String getCountry() {
return country;
}
public String getEmail() {
return email;
}
public void setEmail(final String email) {
this.email = email;
}
}
Sample for Item class:
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
#Id
Long id
//or
//#Id
//String email
Key<RegistrationTO> parent;
String itemno;
}
Thank you in advance!
Regarding your question if the use of e-mail as #Id is correct or not, since the email will uniquely identify each object of the class, then you are fine!
Now, regarding the #Id of your Item class, if the email uniquely identifies each object, then there is no need to create a new auto-generated Long as #Id. In general, the criterion for the selection of the #Id is to uniquely identify all the objects of the class.
For the relationship between RegistrationTO and Item classes, use the #Parent annotation only if you need these entities to be the same entity group. The code for this:
#Parent
Key<RegistrationTO> parent;
Otherwise, use a "plain" relationship (as you have it in your example) that allows RegistrationTO and Item entities to be stored in different entity groups in the GAE datastore. For more information about entity groups, take a look at:
http://code.google.com/appengine/docs/java/datastore/entities.html#Entity_Groups_and_Ancestor_Paths
Hope that helps!