I have a Course entity which contains a Set of Keys to my Tag entity. How would I go about creating a query to get a list of courses with a specific tag? For example, I want to find all the courses tagged with java.
Here are my entities:
#PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Course{
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent private Set<Key> tags;
//etc
}
#PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Tag{
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent private String tagText;
}
Tag tag = getTagFromString("java");
Key tagKey = tag.getKey(); // i will assume you have a getKey() method
PersistenceManger pm = PMF.get().getPersistenceManager();
Query q = pm.newQuery(Course.class);
q.setFilter("tags == :tagParam");
List<Course> coursesTaggedWithJava = (List<Course>) q.execute(tagKey);
I don't think this is possible. Google DataStore does not allow to use Join queries. But I may be mistaken.
Here and here is the website where you can find more information about GQL.
Related
I'm trying to create a Spring Boot backend for my project. In the database I have Deck and Word tables with a many-to-many relationship connected via DeckWord table. The bridge table has additional fields and a composite PK consisting of the other 2 tables' PK's.
I am not sure about how I should structure the crud operations in my project. Say I'm trying to add a new word and it should be assigned to a certain deck. What model's controller should handle the post operation in that scenario: Word or DeckWord? Should the Deck's List<DeckWord> be updated as well?
UPDATE:
Included the models, omitted the getters, setters and constructors
#Entity
#Table(name = "deck")
public class Deck {
#Id
#SequenceGenerator(
name = "deck_sequence",
sequenceName = "deck_sequence",
allocationSize = 1
)
#GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "deck_sequence"
)
#Column(name = "deck_id")
private Long id;
#Transient
private Boolean learnt;
private String name;
#OneToMany(mappedBy = "deck", cascade = CascadeType.ALL)
private List<DeckWord> deckwords;
#ManyToOne
#JoinColumn(name="appuser_id",referencedColumnName="appuser_id")
private Appuser appuser;
}
and
#Entity
#Table(name = "word")
public class Word {
#Id
#SequenceGenerator(
name = "word_sequence",
sequenceName = "word_sequence",
allocationSize = 1
)
#GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "word_sequence"
)
#Column(name = "word_id")
private Long id;
private String definition;
private String transcription;
#OneToMany(mappedBy = "word", cascade = CascadeType.ALL)
private List<DeckWord> deckwords;
}
and the bridge table:
#Embeddable
class DeckWordKey implements Serializable {
#Column(name = "deck_id")
Long deckId;
#Column(name = "word_id")
Long wordId;
}
#Entity
#Table
public class DeckWord {
#EmbeddedId
DeckWordKey id;
#ManyToOne
#MapsId("deckId")
#JoinColumn(name = "deck_id",referencedColumnName="deck_id")
Deck deck;
#ManyToOne
#MapsId("wordId")
#JoinColumn(name = "word_id",referencedColumnName="word_id")
Word word;
private Boolean learnt;
private LocalDate last_checked;
private WordGroup wordGroup;
}
Answering your questions:
What model's controller should handle the post operation in that scenario: Word or DeckWord?
Given that a Word should always be assigned to a Deck, then I would use a POST request to the URL "/decks/{deckId}/words" to create a new Word. The request body should include definition and transcription.
Should the Deck's List be updated as well?
Yes, it must. For that, you need to use deckId that you receive as a path parameter.
I'm developing a Spring Boot REST API application and I've encountered a problem with SQL generation for Transact-SQL (SQL Server) dialect and I'm not sure where I did something wrong.
The application is about storage management and I have two entities: Part and Stock. I've simplified the structure to be as simple as possible.
I have composite PK - PartPK:
#Data #Embeddable
class PartPK {
#Column(name = "PART_ID")
private String partId;
#Column(name = "PART_ORGANIZATION_ID")
private String orgId;
}
... and the entity Part having PartPK as #EmbeddedId:
#Entity #Table(name = "parts")
class Part {
#EmbeddedId
private PartPK id;
}
then I'm having a Stock entity that ties to a Part entity and stores. The entity has a composite PK with the following structure, where I'm overriding attributes from PartPK (giving them STOCK_ prefix)
#Data #Embeddable
class StockPK {
#Column(name = "STOCK_STORE_ID")
private String storeId;
#Embedded
#AttributeOverrides({
#AttributeOverride(name = "partId", column = #Column(name = "STOCK_PART_ID")),
#AttributeOverride(name = "orgId", column = #Column(name = "STOCK_PART_ORGANIZATION_ID")),
})
private PartPK partId;
}
... and enclosing Stock entity where I'm trying to reference the Part entity using #MapsId:
#Entity #Table(name = "stocks")
class Stock {
#EmbeddedId
private StockPK id;
#MapsId("partId")
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumns({
#JoinColumn(name = "STOCK_PART_ID", referencedColumnName = "PART_ID"),
#JoinColumn(name = "STOCK_PART_ORGANIZATION_ID", referencedColumnName = "PART_ORGANIZATION_ID"),
})
private Part part;
}
Which compiles, but after executing a query from the repository, it generates the following query:
select TOP (?)
stockdb0_.stock_part_organization_id as bis_part0_0_,
stockdb0_.stock_store_id as bis_stor3_0_,
stockdb0_.stock_part_organization_id as bis_part5_0_,
stockdb0_.stock_part_id as bis_part6_0_
from stocks stockdb0_
As you can notice, for some reason it uses 2 times stock_part_organization_id column. The entity has incorrect values after persistence mapping (two Stock rows having the same Store but different parts are considered to be the same entity). When the part attribute is removed from the Stock entity, the query and resulting persistence mapping is correct.
Is there anything I'm doing wrong?
I'm using Spring Boot 2.4.5 (the latest) and Started Data Jpa of the same version.
I think using #IdClass will work better in this case:
class StockPK implements Serializable {
private String storeId;
private Part part;
...
}
#Entity #Table(name = "stocks")
#IdClass(StockPK.class)
class Stock {
#Id
private String id;
#Id
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumns({
#JoinColumn(name = "STOCK_PART_ID", referencedColumnName = "PART_ID"),
#JoinColumn(name = "STOCK_PART_ORGANIZATION_ID", referencedColumnName = "PART_ORGANIZATION_ID"),
})
private Part part;
...
}
But if you want to use #EmbeddedId:
#Embeddable
public static class StockPK implements Serializable {
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumns({
#JoinColumn(name = "STOCK_PART_ID", referencedColumnName = "PART_ID"),
#JoinColumn(name = "STOCK_PART_ORGANIZATION_ID", referencedColumnName = "PART_ORGANIZATION_ID"),
})
private Part part;
#Column(name = "STOCK_STORE_ID")
private String storeId;
}
#Entity #Table(name = "stocks")
class Stock {
#EmbeddedId
private StockPK id;
// The association is already defined in the key
}
Anyway, you don't have to use #MapsId (that's for something else) and you can find examples of both approaches with more details in the Hibernate ORM documentation.
I have class to work on PDF
#PersistenceCapable
public class GoogleDrivePDF {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private String pdfName;
... geter, seter...
}
In order to write and read data, I do like this:
GoogleDrivePDF pdf = new GoogleDrivePDF();
key=KeyFactory.createKey(GoogleDrivePDF.class.getSimpleName(),"0B1IQEoiXFg3IWHhJNEtlMzlvQWs");
pdf.setKey(key);
pdf.setPdfName("NAME");
pm.makePersistent(pdf);
and in order to read:
pdf = pm.getObjectById(GoogleDrivePDF.class, "0B1IQEoiXFg3IWHhJNEtlMzlvQWs1");
Everything works!
Now I will create One: Many realationship.
#PersistenceCapable
public class UsersPDFDocuments {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private List<GoogleDrivePDF> pdfs;
... seter, geter...
}
and I write to Database:
List <GoogleDrivePDF> pdfFilesDao=...
GoogleDrivePDF pdf = new GoogleDrivePDF();
Key key= KeyFactory.createKey(GoogleDrivePDF.class.getSimpleName(), "0B1IQEoiXFg3IWHhJNEtlMzlvQWs1");
pdf.setKey(key);
pdf.setVerifyed(false);
pdf.setPdfName("mari123");
pdfFilesDao.add(pdf);
key= KeyFactory.createKey(UsersPDFDocuments.class.getSimpleName(), "mail#mail");
UsersPDFDocuments userData = new UsersPDFDocuments();
userData.setKey(key);
userData.setPdfs(pdfFilesDao);
pm.makePersistent(userData);
OK everything works! BUT I CAN NOT READ PDF DATA NOW!
I have error:
HTTP ERROR 500
Problem accessing /test. Reason:
Could not retrieve entity of kind GoogleDrivePDF with key GoogleDrivePDF("0B1IQEoiXFg3IWHhJNEtlMzlvQWs1")
Caused by:
javax.jdo.JDOObjectNotFoundException: Could not retrieve entity of kind GoogleDrivePDF with key GoogleDrivePDF("0B1IQEoiXFg3IWHhJNEtlMzlvQWs1")
BUT I DO THE SAME , LIKE I WAS DOING:
Key key = KeyFactory.createKey(GoogleDrivePDF.class.getSimpleName(), "0B1IQEoiXFg3IWHhJNEtlMzlvQWs1");
GoogleDrivePDF pdf = pm.getObjectById(GoogleDrivePDF.class, key);
When I was testing, In order to get the value, I need enycrypted String of key. (See image) agtsdHYtY2hlY2tlcnJoCxIRVXNlcnNQREZEb2N1bWVudHMiIXZha2h0YW5nLmtvcm9naGxpc2h2aWxpQGdtYWlsLmNvbQwLEg5Hb29nbGVEcml2ZVBERiIcMEIxSVFFb2lYRmczSVdIaEpORXRsTXpsdlFXcww
when I add Unowned it works! why?!
#Unowned
#Persistent
private List<GoogleDrivePDF> pdfs;
I have an unowned Many to Many relationship setup in JDO by adding the List in both the Persistence Capable objects.
For the sake of explaining my problem lets call these two entities with .
EntityA and EntityB
Now, when i have a new Object of EntityB to be attached to the Object of EntityA, i append that Key to the EntityA object and call makePersistent on it, which saves the object.
I verified that by printing it on the console.
Since, this is a Many to Many relation, i have to do the same on the other end of the relation as well.
So, i fetch all the objects of EntityB which are referred by EntityA using
select from " + clazz.getName()+ " where :keys.contains(key) and passing it the List of Keys which are present in Object of EntityA.
The problem that i encounter is, the objects returned back are Hollow, and hence they dont get saved into the datastore even if i append the EntityA keys to the fetched objects.
I am a newbie in JDO and GAE, and have been facing this problem since yesterday.
Can someone please shed some light on this? I can provide sample code if needed too.
Here is the code
#PersistenceCapable
public class Objective {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private boolean active;
#Persistent
private int corporate;
#Persistent
private String nameOfObjective;
#Persistent
private String shortDescription;
#Persistent
private int status;
#Persistent
private List<Key> scoreCardKeys; //List of Keys of Scorecards.
#PersistenceCapable
public class Scorecard {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private boolean active;
#Persistent
private int corporate; // synonymous to being public
#Persistent
private Date creationDate;
#Persistent
private String nameOfScorecard;
#Persistent
private String shortDescription;
#Persistent
private Key createdUserKey;
#Persistent
private List<Key> objectiveKeys; // List of Keys of Objectives
Objective and Scorecard entities are in an unowned Many to Many relation
Here is the processor method which will update a Scorecard.
public ScoreCardRepresentation updateScoreCard(ScoreCardRepresentation scoreCardRepresentation) {
Scorecard scoreCard = scoreCardTransformer
.transformRtoEForSave(scoreCardRepresentation);
scoreCard.setCreationDate(new Date());
Scorecard updatedScoreCard = scoreCardDAO.saveScoreCard(scoreCard); /* Update the scorecard, this already has the list of Key of Objectives in it, Hence blindly save it. */
/* Update the Key of the scorecard in the Objectives too */
updateRelatedObjectivesToScoreCard(scoreCardRepresentation,updatedScoreCard);
private void updateRelatedObjectivesToScoreCard(
ScoreCardRepresentation scoreCardRepresentation,
Scorecard updatedScoreCard) {
List<String> addedObjectivesIds = scoreCardRepresentation.getAddedObjectiveKeys();
List<String> deletedObjectivesIds = scoreCardRepresentation.getRemovedObjectiveKeys();
// Add ScoreCard to the newly added Objectives
if(addedObjectivesIds != null && addedObjectivesIds.size()>0){
Scorecard sc = scoreCardDAO.findScoreCardById(Scorecard.class, updatedScoreCard.getKey());
List<Key> objKeys = sc.getObjectiveKeys();
List<Objective> objectives = objectiveDAO.findObjectivesByKeys(Objective.class,objKeys);
// This uses the query select from " + clazz.getName()+ " where :keys.contains(key)
for(Objective obj : objectives){
List<Key> scoreCardKeys = obj.getScoreCardKeys();
if(scoreCardKeys != null){
scoreCardKeys.add(sc.getKey());
} else {
scoreCardKeys = new ArrayList<Key>();
scoreCardKeys.add(sc.getKey());
}
obj.setScoreCardKeys(scoreCardKeys);
Objective updatedObjective = objectiveDAO.saveObjective(obj);
System.out.println(new ObjectiveProcessor().viewObjective(KeyFactory.keyToString(obj.getKey())));
}
}
//Remove Scorecard entries from Objective.
if(deletedObjectivesIds != null && deletedObjectivesIds.size()>0){
List<Objective> objectives = objectiveDAO.findObjectivesByIds(Objective.class,deletedObjectivesIds);
for(Objective obj : objectives){
List<Key> scoreCardKeys = obj.getScoreCardKeys();
if(scoreCardKeys != null){
scoreCardKeys.remove(updatedScoreCard.getKey());
}
obj.setScoreCardKeys(scoreCardKeys);
}
}
}
All i have been able to realise is that when i get back the Objectives using **findObjectivesByKeys** i am getting back hollow objects, so i have to call makeTransient on them to enable them for persistence, else they just ignore the makePersistent method call.
I have the following code:
#PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class A {
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
#PrimaryKey
private Key key;
#Persistent
private B b;
#Persistent
private int id;
// ...
}
#PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class B {
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
#PrimaryKey
private Key key;
#Persistent
private int id;
// ...
}
Now what I need to be able to do, is retrieve an instance of B, and refer to it from an instance of A like this:
B b = DAL.getBById(1);
A a = new A();
a.setB(b);
When I pass a to the makePersistent() method of the PersistenceManager, two things that I don't need happen:
1) a new instance of B is created
2) the reference A makes to b is null
Could someone tell me what I am doing wrong?
Thanks!
A field value can contain an instance of a Serializable class, storing the serialized value of the instance in a single property value of the type Blob. To tell JDO to serialize the value, the field uses the annotation #Persistent(serialized=true). Blob values are not indexed and cannot be used in query filters or sort orders.
Here is an example of a simple Serializable class that represents a file, including the file contents, a filename and a MIME type. This is not a JDO data class, so there are no persistence annotations.
import java.io.Serializable;
public class DownloadableFile implements Serializable {
private byte[] content;
private String filename;
private String mimeType;
// ... accessors ...
}To store an instance of a Serializable class as a Blob value in a property, declare a field whose type is the class, and use the #Persistent(serialized = "true") annotation:
import javax.jdo.annotations.Persistent;
import DownloadableFile;
// ...
#Persistent(serialized = "true")
private DownloadableFile file;
I your case you can use
import java.io.Serializable;
public class B implements Serializable {
private int xx;
....
..........
}
Then declare it in your data class
#Persistent(serialized = "true")
private B b;