When using OrientDB Object API in schemaless mode, is it possible to have a field in a POJO marked as unique?
/**
The entity
*/
public class FooEntity {
#Column(unique=true)
private String bar;
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
And this is how the entity is registered, somewhere in another location in the code:
oObjectDatabase.getEntityManager().registerEntityClasses(FooEntity.class.getName());
OrientDB mechanism to map entities does not take into account the JPA annotations, but how could the same effect be achieved?
Would it be the best solution to add the constraint programmatically to the schema after registering the entity?
To add some constraint like unique (that is done through an unique index) you need to define the property in the schema, this means that you cannot have a unique constraint and be schema-less.
And yes for the OrientDB object 2.2.x and before the best way to define unique property is doing some programmatically or scripting schema definition after you registered the entity.
Bye
Related
Problem with: Dapper Extensions dbConnection.Get(personId)
I have a model called Person:
Person
{
public int PersonId { get; set; }
public string Name { get; set; }
}
In the database I have this table:
data.Persons
Pers_Id
Pers_Name
When I try this without any kind of custom mapping, I get an error "Invalid object name 'Person'."
I believe this is a mapping issue, because when I completely map the model with the prefix 'Pers_', and use 'data.Persons'
Get works.
But is there a way to automatically map with a prefix? The database I'm using has many different tables
with different prefices.
I also have everything already mapped to Entity framework. Is there a possibility of getting the map settings from
Entity DbModelBuilder?
Dapper-Extensions is convention based. For schema, it uses .dbo and for primary key it uses Id. If your tables don't match the convention, you will have to create a custom mapping.
public class MyModelMapper : ClassMapper<MyModel>
{
public MyModelMapper()
{
//use different table name
Table("table_name");
//use a custom schema
Schema("not_dbo_schema");
//have a custom primary key
Map(x => x.ThePrimaryKey).Key(KeyType.Assigned);
//Use a different name property from database column
Map(x=> x.Foo).Column("Bar");
//Ignore this property entirely
Map(x=> x.SecretDataMan).Ignore();
//optional, map all other columns
AutoMap();
}
}
An alternative is to use Dapper and just write your inline queries:
connection.Query("select * from foo.table where myId = {myId}", new {myId})
Update:
Another alternative is to play around with Code Generation and T4 Text Templates
Here is a trivial example
I have already tried use polymorphic in greenDao, but not successful.
How to use polymorphic in greenDao ?
for example:
I have a table "my_favorites", in that i can store id foreign key of Events, Pictures, Music ... ?
I hope for your help!
I'm afraid this is not supported by greendao at the moment.
You will have to store the keys to a normal 'LongProperty' without any 'toOne' or 'toMany' and in a second column you will have to save a discriminator value (i.e. the referenced classname).
Then you will have to build the queries yourself (i.e. in the 'KEEP-SECTION' of your 'my_favorites'.
public Object getDetail() throws ClassNotFoundException {
if (daoSession== null) {
throw new DaoException("Entity is detached from DAO context");
}
return daoSession.load(Class.forName(getDiscriminator()), getMyDetailId());
}
you can use ToMany relation in your DB. Look at this tutorial
Entity customer = schema.addEntity("Customer");
customer.addIdProperty();
customer.addStringProperty("name").notNull();
Entity order = schema.addEntity("Order");
order.setTableName("ORDERS"); // "ORDER" is a reserved keyword
order.addIdProperty();
Property orderDate = order.addDateProperty("date").getProperty();
Property customerId = order.addLongProperty("customerId").notNull().getProperty();
order.addToOne(customer, customerId);
ToMany customerToOrders = customer.addToMany(order, customerId);
customerToOrders.setName("orders");
customerToOrders.orderAsc(orderDate);
My problem is to persist two classes that have a 1:n relationship:
public class DayRecord {
private Long id;
private List<TimeRecord> timeRecordsToday = new ArrayList<TimeRecord>(4);
...
}
public class TimeRecord {
private Long id;
...
}
So, in code, DayRecord knows TimeRecord.
create table DAY_RECORDS (
id int primary key,
);
create table TIME_RECORDS (
id int primary key,
day_record_id int not null,
foreign key (day_record_id) references DAY_RECORDS (id)
);
In database, TimeRecord knows DayRecord.
Can I save a DayRecord with all its TimeRecords in one step?
In Hibernate, I can set an inverse mapping and just save a DayRecord and all its TimeRecords will get saved, too. With MyBatis, I tried to save the classes independently from each other:
<mapper
namespace="de.stevenschwenke.java.javafx.xyz.DayRecordMapper">
<insert id="insertDayRecord"
parameterType="de.stevenschwenke.java.javafx.xyz.DayRecord">
insert into DAY_RECORDS (id) values (NEXT VALUE FOR DAY_RECORDS_SEQ);
</insert>
</mapper>
<mapper
namespace="de.stevenschwenke.java.javafx.xyz.TimeRecordMapper">
<insert id="insertTimeRecord"
parameterType="de.stevenschwenke.java.javafx.xyz.TimeRecord">
insert into TIME_RECORDS (id) values (NEXT VALUE FOR TIME_RECORDS_SEQ);
</insert>
</mapper>
But how can I save the DayRecord-ID inTimeRecord?
Ideas:
Give TimeRecord an attribute dayRecordId. This way, a cyclic dependency would be created. However, the mapping would take care of the dpenedency while saving.
In one transaction, save the DayRecord first, get its ID, set it in TimeRecords and save this object.
use a nested select-statement within insert like in the documentation
What is the best way to save both objects? Thanks for your help!
As jdevelop already mentioned, MyBatis is just a SQL wrapper. Because SQL doesn't offer a way to insert two objects that have a relationship, MyBatis can't do that either.
So here's my workaround: As I mentioned, I don't want to add a circular dependency by letting TimeRecord know about DayRecord. So I created a wrapper class just for inserting TimeRecords:
public class TimeRecordInsertWrapper {
public Long id;
public int hours;
public long dayRecordId;
[constructor/getter/setter omited but there with public access modifier]
}
First, I store the DayRecord and get it's ID. Then I create the wrapper object and store the TimeRecords:
public long insertDayRecord(DayRecord newRecord) {
SqlSession session = sqlSessionFactory.openSession();
try {
session.insert(
"de.stevenschwenke.java.javafx.xyz.DayRecordMapper.insertDayRecord",
newRecord);
for (TimeRecord tr : newRecord.getTimeRecordsToday()) {
TimeRecordInsertWrapper wrapper = new TimeRecordInsertWrapper(tr.getHours(), newRecord.getId());
session.insert("de.stevenschwenke.java.javafx.xyz.TimeRecordMapper.insertTimeRecord",
wrapper);
}
return newRecord.getId();
} finally {
session.commit();
session.close();
}
}
This way, I can use my nice one-way object model AND have the "right" mapping in the database.
Mybatis is just SQL mapping framework, it allows you to abstract SQL code from Java code and that's it, more or less. They are pretending to look like Hibernate with recent versions, but this leads to weird constructions in XML.
I would suggest to store the DayRecord and get it's it from selectKey, then use that ID in subsequent calls to the mapper. This is what actually happens inside the mapper, but complex XML implies complex FSM to built inside. So keep it simple and you're safe with myBatis, or use Hibernate.
What is even better, you can define custom DAO interfaces for the tasks, and then you can have some sort of Service layer with #Transactional attribute set. This requires mybatis-guice, but it works really great and you don't need to deal with transactions in your code (they are declarative).
I'd like to know how to create a method that will allow me to generically do this...
public class Repo<T> : IGenericRepo<T> where T : class
{
protected PteDotNetEntities db;
public T Get(int id)
{
//how do I dynamically get to the correct entity object and select it by
//id?
}
}
Yes you can. If you know that all your entities will have simple primary key property of type int and name Id you can do simply this:
public interface IEntity
{
int Id { get; }
}
All your entities must implement this interface. Next you can simply do:
public class Repo<T> : IGenericRepo<T> where T : class, IEntity
{
protected PteDotNetEntities db;
public T Get(int id)
{
return db.CreateObjectSet<T>().FirstOrDefault(e => e.Id == id);
}
}
This is the simplest possible solution. There are better solutions using GetObjectByKey but they are more complex. The difference between FirstOrDefault and GetObjectByKey is repeatable execution. FirstOrDefault always executes DB query whereas GetObjectByKey first checks if the entity with the same key was already loaded / attached to the context and returns it without querying the database. As reference for version using GetObjectByKey you can check similar questions:
Entity Framework Simple Generic GetByID but has differents PK Name
Generic GetById for complex PK
You can simplify those examples if you know the name of the key property upfront (forced by the interface).
In case of using code first / DbContext API you can also check this question:
Generic repository EF4 CTP5 getById
When creating POCO classes that contain collections of primitive types and are persisted by EF Code First, the best advice I have found so far is to create a new class that has an ID plus the primitive type:
Entity Framework and Models with Simple Arrays
If I now have several classes that require properties of type ObservableCollection<string> and replace them with ObservableCollection<EntityString> (where EntityString is a custom type with an Id and a string property), I end up with a table EntityString that has multiple foreign key columns, one for each property of type ObservableCollection<EntityString> across all concrete types with such properties.
This leads to a bloating of mostly-null foreign key columns in the EntityString table.
One approach would be to create a subclass of EntityString and use the Table per Type model for those subclasses. However, that requires making awkward changes to the object model simply to accommodate Entity Framework.
Questions:
Is the encapsulating type the best way to manage Collection<PrimitiveType>?
If so, what are the pro's and con's of allowing multiple (many) foreign key columns vs. creating custom tables per type (at the cost of an awkward model)?
Promoting simple type to entity is one option. If you want to use that new primitive type entity in more relations it is better to completely remove navigation properties from that entity and use independent association (no FK properties).
public class StringEntity
{
public int Id { get; set; }
public string Text { get; set; }
}
and mapping:
modelBuilder.Entity<Foo1>().HasMany(f => f.Strings).WithOptional();
modelBuilder.Entity<Foo2>().HasMany(f => f.Strings).WithOptional();
In database you will get new nullable FK per related principal - there is no way to avoid it except create special StringEntity class per principal (don't use inheritance for that because it affects performance).
There is an alternative:
public class StringEntity
{
public int Id { get; set; }
public List<string> Strings { get; private set; }
public string Text
{
get
{
return String.Join(";", Strings);
}
set
{
Strings = value.Split(";").ToList();
}
}
}
In this case you don't need related entity type (and additional table) but your entity is polluted with additional property Text which is only for persistence.