This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unique Constraint At Data Level in GAE
Does the Google App engine datastore support unique constraints?
Not really, but keys are guaranteed to be unique and you can set the key of your entity to be an arbitrary string. You can take advantage of this to create a unique key when you save your entity to the data store. For example,
public class Entity implements Serializable {
#Id String id;
String unique_together_1;
String unique_together_2;
public Entity (String unique_together_1, String unique_together_2) {
this.id = unique_together_1 + "-" + unique_together_2;
this.unique_together_1 = unique_together_1;
this.unique_together_2 = unique_together_2;
}
Obviously this won't work if the unique fields of your entity change later or if you need multiple unique constraints for a single entity type.
Related
Here's what I have:
class A{
Ref<foo> b;
Ref<foo> c;
int code;
Date timestamp
}
The pseudo "where" clause of the SQL statement would like like this:
where b = object or (c = object and code = 1) order by timestamp
In plain English, give me all the records of A if b equals the specific object or if c equals the specified object when code equals 1. Order the result w/ timestamp.
Is the composite query part even possible w/ datastore (Objectify)? I really don't want to do two queries and merge the results, because I have to sort by timestamp.
Any help is appreciated.
P.S. I already tried
new FilterPredicate(b, EQUAL, object)
This didn't work, because the entity type is not a support type.
Thanks!
Pass a native datastore Key object to the FilterPredicate. The Google SDK Key, not the generic Objectify Key<?>.
Normally when filtering on properties, Objectify translates Ref<?> and Key<?> objects to native datastore keys for you. With the google-supplied FilterPredicate, that isn't an option. So you have to do the translation manually.
Objectify stores all Key<?> and Ref<?> fields and native datastore Keys, so you can freely interchange them (or even change the type of fields if you want).
I have an Entity as
#Entity
public class Book{
...
List<Key<Page>> pages;
...
}
So to get a book I do
Book book = ofy().load().type(Book.class).id(id).now();
Having obtained the book, I want to get the pages; hence my question: can I query by keys or must I query by ids? If I had the ids I could do
List<Page> pages = ofy().load().type(Page.class).ids(ids);
But what I need is
List<Page> pages = ofy().load().type(Page.class).keys(keys);
otherwise I have to do linear work to iterate through the keys to extract the ids or the names, which I am not even sure will work because the keys actually have parents so that a key for a page is constructed as
Key pageKey = KeyFactory.createKey(bookKey, Page.class.getSimpleName(),someString);
So what is my final answer in this case?
You can easily load entities by Keys. From ofy's Concepts page (https://code.google.com/p/objectify-appengine/wiki/Concepts)
Map<Key<Object>, Object> lotsOfThings = ofy().load().keys(carKey, airplaneKey, chairKey, personKey, yourMamaKey);
I want to use UUIDs as primary due to the fact that those primaries are globally unique which makes it (for example) easy to integrate data from a production environment into a running debug environment.
Regarding to the following article: http://iops.io/blog/storing-billions-uuid-fields-mysql-innodb SELECT/INSERT of millions of records into a table using a UUID decoded as BINARY(16) is significantly faster than using simple CHAR(36).
Now, using Hibernate #GenericGenerator annotation, I could use this native UUID generator for a primary key using UUIDs:
#Id
#GeneratedValue(generator = "system-uuid")
#GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;
On the other hand I could define a binary UUID as primary as follows:
#Id
#Getter
#Column(columnDefinition = "BINARY(16)", length = 16, updatable=false, nullable=false)
private byte[] id;
... and use #PrePersist to generate new UUIDs
#PrePersist
private void prePersist() {
if (this.id == null) {
this.generateUUID();
}
}
The problem with this solution is the representation as binary for filters within (native/named) queries:
SELECT * from object o WHERE o.id=:id
What I would really need is the ability store the UUID field as above-mentioned as BINARY within the database while representing the value as simple UUID-String.
Is there any way to do that? Is there any alternative?
Why don't you just use the special uuid type for the column?
#Type(type = "pg-uuid")
But I also have the problem with native queries when doing like that.
I have a Spring application which uses JPA (Hibernate) initially created with Spring Roo. I need to store Strings with arbitrary length, so for that reason I've annotated the field with #Lob:
public class MyEntity{
#NotNull
#Size(min = 2)
#Lob
private String message;
...
}
The application works ok in localhost but I've deployed it to an external server and it a problem with encoding has appeared. For that reason I'd like to check if the data stored in the PostgreSQL database is ok or not. The application creates/updates the tables automatically. And for that field (message) it has created a column of type:
text NOT NULL
The problem is that after storing data if I browse the table or just do a SELECT of that column I can't see the text but numbers. Those numbers seems to be identifiers to "somewhere" where that information is stored.
Can anyone tell me exactly what are these identifiers and if there is any way of being able to see the stored data in a #Lob columm from a pgAdmin or a select clause?
Is there any better way to store Strings of arbitrary length in JPA?
Thanks.
I would recommend skipping the '#Lob' annotation and use columnDefinition like this:
#Column(columnDefinition="TEXT")
see if that helps viewing the data while browsing the database itself.
Use the #LOB definition, it is correct. The table is storing an OID to the catalogs -> postegreSQL-> tables -> pg_largeobject table.
The binary data is stored here efficiently and JPA will correctly get the data out and store it for you with this as an implementation detail.
Old question, but here is what I found when I encountered this:
http://www.solewing.org/blog/2015/08/hibernate-postgresql-and-lob-string/
Relevant parts below.
#Entity
#Table(name = "note")
#Access(AccessType.FIELD)
class NoteEntity {
#Id
private Long id;
#Lob
#Column(name = "note_text")
private String noteText;
public NoteEntity() { }
public NoteEntity(String noteText) { this.noteText = noteText }
}
The Hibernate PostgreSQL9Dialect stores #Lob String attribute values by explicitly creating a large object instance, and then storing the UID of the object in the column associated with attribute.
Obviously, the text of our notes isn’t really in the column. So where is it? The answer is that Hibernate explicitly created a large object for each note, and stored the UID of the object in the column. If we use some PostgreSQL large object functions, we can retrieve the text itself.
Use this to query:
SELECT id,
convert_from(loread(
lo_open(note_text::int, x'40000'::int), x'40000'::int), 'UTF-8')
AS note_text
FROM note
I am trying to build a generic repository using Entity Framework 4.0 using a legacy MS SQL database I have inherited. A pretty familiar scenario.
I need to add category information to a fairly long list of existing items.
The items can belong to several categories at the same time so I created an mapping table called CategoryMapping
Unfortunately SchemaDefinitionCode is not unique and cannot be made into a Foreign Key (FK) in the database.
I have tried to add my own partial class to the DefinitionSchema entity but as it's not indexed, this has a severe performance hit. Demo code for testing, I won't want to create a new context every time I load this:
public partial class DefinitionSchema
{
private MyEntities context;
public IQueryable<Category> Categories
{
get
{
context = new MyEntities();
var categories = context.Categories
.Where(c => c.CategoryMappings
.Where(m => m.SchemaDefinitionCode == this.SchemaDefinitionCode).Any());
return categories;
}
}
}
I can then call a list of items like so:
var q = context.SchemaDefinitions
.Where(s => s.Categories
.Where(c => c.Name == category)
.Any()
);
How can I link my tables and mapping in the most efficient manner without wiping out the existing database structure?
It can't work this way because EF doesn't support unique keys and SchemaDefinitionCode must be unique to form valid many-to-many relation with Category. If your SchemaDefinitionCode is not unique in DefinitionSchema table it can't be used as principal end in the relation with CatagoryMapping. If it is unique you can use SchemaDefinitionID instead because no more then one Id will have the same code value.