Foreign Key Hibernate Configuration - database

i'm trying to use hibernate with an existing mysql db using Eclipse.
I've succeeded mapping tables to classes and executing some query.
But i have a problem with a one to many relation.
I have the table "CARATTERISTICHE" (attributes) which is, in fact, a tree, described by a join table "VALORI" (values): "fk_child" "fk_parent".
I would like that the class Caratteristica had a field "children" with type List<Caratteristica>, where the children should be the join with CARATTERISTICA and VALORI.
My first attempt was to create a pojo Caratteristica and let eclipse generate the configurations file of hibernate.
That doesn't work because when I launch getChildren all i got is the same object (i.e. every node is his father, which is false in my db).
This is an excerpt of the generated xml:
<class name="model.Caratteristica" table="caratteristiche">
<id name="id" type="java.lang.Integer">
<column name="ID"/>
<generator class="assigned"/>
</id>
<set access="field" lazy="true" name="children"
sort="unsorted" table="valori">
<key>
<column name="id"/>
</key>
<one-to-many class="model.Caratteristica"/>
</set>
Note that if change the column key from id to fk_child he can't find fk_child in table "CARATTERISTICHE" (but it should look in VALORI, right?)
I've also tried to generate pojos from tables but it's worse..
Maybe i've got this problem hibernate- composite key configuration but.. it's my first time I use hibernate, I'm really lost!

Your data model is a bit strange.
Either you have a complete tree structure, i. e. a 1 : n relation between parents and children. Then you do not need the join table VALORI. In CARATTERISTICHE you just define a column parentId, which contains the id of the parent row. In your mapping you give this new column as the key tag: <key> <column name="parentId"/> </key>
Or you have an m : n relation between parents and children, which is defined by the join table VALORI. The tree structure can be defined as a special case with the m : n relation, but other structures are also possible. Then you must use the <many-to-many> tag (instead of one-to-many). The <many-to-many> tag contains the column attribute, which allows to specify the name of the corresponding foreign key in the other table.

Related

Solr not returning all indexed documents

I have encountered problem that Solr is not returning all indexed documents.
After completing full-import than going to web UI's Schema Browser there is correct information about document count, but when querying for all, there are ~400 documents missing. Following 2 pictures show Schema Browser and Query views
Loading Term Info also shows all documents and even those which are not shown with queries. After click on term the query is made that return nothing.
Database ID's are unique and can not possibly overlap.
Here's code fragment from data-config
<entity name="PZ" pk="DOKUMENTA_ID" transformer="TemplateTransformer, ClobTransformer"
query="
SELECT pz.dokumenta_id,
... // selecting other non pk fields
">
<field column="primaryKey" template="${PZ.DOKUMENTA_ID}" />
<field column="DOKUMENTA_ID" name="pz_id" />
...
primaryKey is defined in schema.xml as unique key

Why doesn't Google Cloud DataStore support multiple inequality filter on different properties?

I know that is a limitation of DataStore, But I just want to figure out the reason.
Invalid Argument: Cannot have inequality filters on multiple properties: [..., ...]
I have read the bigtable paper and I can not find any restriction on inequality filter on different column. and it can support prefix and range scan. IMHO, DataStore could support that multiple inequality filter with these two operation. Do you know any reason take the functionality from DataStore?
To avoid having to scan the entire index table, the query mechanism relies on
all of a query's potential results being adjacent to one another in the index.
To satisfy this constraint, a single query may not use inequality comparisons
(LESS_THAN, LESS_THAN_OR_EQUAL, GREATER_THAN, GREATER_THAN_OR_EQUAL, NOT_EQUAL)
on more than one property across all of its filters
[Source : https://cloud.google.com/appengine/docs/standard/java/datastore/query-restrictions ]
Every datastore query is running on one or more index table.
When considering inequality filters, we want to run on at most one property in order to avoid running on the entire index tables. It's easier to grasp when investigating how Filters work, how datastore-indexes.xml defined and what is index exactly:
<datastore-index> are elements, one for every index that the Datastore should maintain. So, you defining one index (which is a table) for every relation between Entity's kind and it's properties. Now imagine when you need to calculate multiple inequality filter on different properties? It can lead to a huge running time. Not good.
<?xml version="1.0" encoding="utf-8"?>
<datastore-indexes autoGenerate="true">
<datastore-index kind="Greeting" ancestor="true" source="manual">
<property name="user" direction="asc" />
<property name="birthYear" direction="asc" />
<property name="height" direction="asc" />
</datastore-index>
<datastore-index kind="Greeting" ancestor="true" source="manual">
<property name="date" direction="asc" />
</datastore-index>
</datastore-indexes>
Now, let's see filters.
It's a valid filter - he runs on one property only (the birthYear property).
Filter birthYearMinFilter =
new FilterPredicate("birthYear",
FilterOperator.GREATER_THAN_OR_EQUAL,
minBirthYear);
Filter birthYearMaxFilter =
new FilterPredicate("birthYear",
FilterOperator.LESS_THAN_OR_EQUAL,
maxBirthYear);
Filter birthYearRangeFilter =
CompositeFilterOperator.and(birthYearMinFilter, birthYearMaxFilter);
Query q = new Query("Person").setFilter(birthYearRangeFilter);
It's a non-valid filter, he runs on 2 properties (birthYear and height):
Filter birthYearMinFilter =
new FilterPredicate("birthYear",
FilterOperator.GREATER_THAN_OR_EQUAL,
minBirthYear);
Filter heightMaxFilter =
new FilterPredicate("height",
FilterOperator.LESS_THAN_OR_EQUAL,
maxHeight);
Filter invalidFilter =
CompositeFilterOperator.and(birthYearMinFilter, heightMaxFilter);
Query q = new Query("Person").setFilter(invalidFilter);

solr sort on an unrelated entity field

My document structure is like this
<document>
<entity name="entity1" query="query1">
<field column="column1" name="column1" />
<!-- more columns specific to this entity -->
</entity>
<entity name="entity2" query="query2">
<field column="column2" name="column2" />
<!-- more columns specific to this entity -->
</entity>
</document>
In my query involving entity1 columns only, if I add entity2 columns in sort clause, why should the result be affected at all? My query is only on entity1 columns which are unrelated to entity2. Is it the case that solr apply the sort clause first on entire "documents" and then apply the query condition(s)?
Documentation reads -
If sortMissingLast="false" and sortMissingFirst="false" (the default),
then default lucene sorting will be used which places docs without the
field first in an ascending sort and last in a descending sort.
Can someone please elaborate on the bolded text?
I think the last paragraph of my question had the answer in it.
If field is missing, default sorting is used which is why my results look "affected".

Solr: How distinguish between multiple entities imported through DIH

When using DataImportHandler with SqlEntityProcessor, I want to have several definitions going into the same schema with different queries.
How can I search both type of entities but also distinguish their source at the same time. Example:
<document>
<entity name="entity1" query="query1">
<field column="column1" name="column1" />
<field column="column2" name="column2" />
</entity>
<entity name="entity2" query="query2">
<field column="column1" name="column1" />
<field column="column2" name="column2" />
</entity>
</document>
How to get data from entity 1 and from entity 2?
As long as your schema fields (e.g. column1, column2) are compatible between different entities, you can just run DataImportHandler and it will populate Solr collection from both queries.
Then, when you query, you will see all entities combined.
If you want to mark which entity came from which source, I would recommend adding another field (e.g. type) and assigning to it different static values in each entity definition using TemplateTransformer.
Also beware of using clean command. By default it deletes everything from the index. As you are populating the index from several sources, you need to make sure it does not delete too much. Use preImportDeleteQuery to delete only entries with the same value in the type field that you set for that entity.

AdventureWorks can't create ado.net entity data model

I'm trying to use microsoft's sample database AdventureWorks2008R2... when I try to create the ADO.NET entity data model I get this error:
Unable to generate the model because of the following exception: 'The table 'C:\USERS\XXXX\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\ANOTHERWORKS\ANOTHERWORKS\APP_DATA\ADVENTUREWORKS2008R2_DATA.MDF.Production.Document' was referenced by a relationship, but was not found.'.
Loading metadata from the database took 00:00:06.2308687.
Generating the model took 00:00:04.5808698.
Added the connection string to the Web.Config file.
Successfully registered the assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the Web.Config file.
Writing the .edmx file took 00:00:00.0015898.
Anyone encountered and fixed this? Or knows somewhere I can download a working version of this database ( other then: http://msftdbprodsamples.codeplex.com/releases/view/59211 which is where I got it )
Or can someone point to a sample database I can download and use around with entity framework.
EDIT
The new EF designer (Beta 1 available here) will not try creating relationships to non-existing tables so instead of the error and empty model you will get a model where invalid entity types/sets and relationships are commented out.
**
I looked at it for AdventureWorks for Sql Server 2012 but I think it is the same thing you hit for 2008R2/
The issue here is that the Production.Document table has a key that is of HierarchyId type and EF currently does not support HierarchyId type. EF ignores columns of types it does not understand when creating the model however if it does not understand a key column it will exclude the entire entity from the model. For excluded entities you should be able to find them commented out in the model when you open it with an Xml/Text editor. In this particular case this is what will see:
<EntityContainer Name="AdventureWorksModelStoreContainer" />
<!--Errors Found During Generation:
warning 6005: The data type 'hierarchyid' is currently not supported for the target .NET Framework version; the column 'DocumentNode' in table 'AdventureWorks.Production.Document' was excluded.
warning 6031: The column 'DocumentNode' on the table/view 'AdventureWorks.Production.Document' was excluded, and is a key column. The table/view has been excluded. Please fix the entity in the schema file, and uncomment.
<EntityType Name="Document">
<Property Name="DocumentLevel" Type="smallint" StoreGeneratedPattern="Computed" />
<Property Name="Title" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="Owner" Type="int" Nullable="false" />
<Property Name="FolderFlag" Type="bit" Nullable="false" />
<Property Name="FileName" Type="nvarchar" Nullable="false" MaxLength="400" />
<Property Name="FileExtension" Type="nvarchar" Nullable="false" MaxLength="8" />
<Property Name="Revision" Type="nchar" Nullable="false" MaxLength="5" />
<Property Name="ChangeNumber" Type="int" Nullable="false" />
<Property Name="Status" Type="tinyint" Nullable="false" />
<Property Name="DocumentSummary" Type="nvarchar(max)" />
<Property Name="Document" Type="varbinary(max)" />
<Property Name="rowguid" Type="uniqueidentifier" Nullable="false" />
<Property Name="ModifiedDate" Type="datetime" Nullable="false" />
</EntityType>-->
</Schema>
Notice this warning:
warning 6031: The column 'DocumentNode' on the table/view 'AdventureWorks.Production.Document' was excluded, and is a key column. The table/view has been excluded. Please fix the entity in the schema file, and uncomment.
Now in the AdventureWorks database the Production.Document table is referenced by Production.ProductDocument table. Since no entity for Production.Document table was created EF is unable to create the reference from Production.ProductDocument entity and hence the "Production.Document' is referenced by a relationship, but cannot be found.' error.
Since the Production.Document table cannot be really used "as is" by EF the easiest workaround is to exclude this entity when generating the model from entity - check all tables but Production.Document in the wizard and you should be good to go. EF will ignore all references to this entity since you excluded it and therefore there should be no error.
Link to a related work item on Entity Framework codeplex site

Resources