Many-to-many relationship with columns in the association in propel - database

I want to have in generated by Propel objects function e.g. getUsersPrivileges(). I have two tables Users and Privileges described as;
<table name="users" phpName="User">
<column name="user_id" type="integer" required="true" autoIncrement="true" primaryKey="true" />
<column name="login" type="longvarchar" required="true" />
<column name="password" type="longvarchar" required="true" />
<column name="email" type="longvarchar" required="true" />
<column name="language" type="longvarchar" required="true" defaultValue="en" />
<column name="salt1" type="longvarchar" required="true" />
<column name="salt2" type="longvarchar" required="true" />
<column name="date_created" type="timestamp" required="true" />
<column name="archived" type="integer" required="true" defaultValue="0" />
</table>
<table name="privileges" phpName="Privilege">
<column name="privilege_id" type="integer" required="true" autoIncrement="true" primaryKey="true" />
<column name="name" type="longvarchar" required="true" />
<column name="description" type="longvarchar" required="true" />
</table>
<table name="users_privileges" phpName="UserPrivilege" isCrossRef="true">
<column name="user_privilege_id" type="integer" required="true" autoIncrement="true" unique="true" primaryKey="true" />
<column name="user_id" type="integer" />
<column name="privilege_id" type="integer" />
<column name="archived" type="integer" required="true" defaultValue="0" />
<foreign-key foreignTable="users">
<reference local="user_id" foreign="user_id"/>
</foreign-key>
<foreign-key foreignTable="privileges">
<reference local="privilege_id" foreign="privilege_id"/>
</foreign-key>
</table>
I want to have a user_privilege_id as primary key in SQL schema, but Propel requires to generated this method that columns user_id and privilege_id as primary key and foreign key to users.user_id and privilege_id to privileges.privilege_id. It is good to have any additional column in this table.
How to tell Propel to join these tables through these columns and no primary key?

It doesn't work. PrimaryKeys are the only way Propel knows which relations from left to right should created. If you add additional PKs it will create different relation methods. If you have a completely different PK Propel is unable to check which outgoing relations from the cross-table needs to be cross linked from user to privileges and vice-versa. If you really need a ID, you can add this id column as normal with a unique constrain and auto-increment.
Or alternative, you don't use isCrossRef and write your relation methods from user to privileges and vice-versa on your own.

Related

SQL Templating in SmartGWT

I am trying to use SQL templating in smartgwt to load data from my database into a listgrid but I am not able to get the desired result. This is the raw SQL query which I am trying to adopt in SmartGWT to get the result
SELECT
dbo.province.provincename as province,
dbo.province.capital,
dbo.province.code,
dbo.province.telcode,
dbo.province.taxcode,
dbo.county.countyname as county,
dbo.district.districtname as district,
dbo.zone.alternateName as zone,
dbo.neighbourhood.alternateName as neigbhour,
dbo.city.cityname as city,
dbo.city.taxcode,
dbo.city.fdocode,
count(dbo.customer.customeraltname) as countCustomer
FROM
dbo.county
INNER JOIN
dbo.province
ON
(
dbo.county.provinceID = dbo.province.id)
INNER JOIN
dbo.district
ON
(
dbo.county.id = dbo.district.countyID)
INNER JOIN
dbo.city
ON
(
dbo.district.id = dbo.city.districtID)
INNER JOIN
dbo.zone
ON
(
dbo.city.id = dbo.zone.cityId)
INNER JOIN
dbo.neighbourhood
ON
(
dbo.zone.id = dbo.neighbourhood.zoneId)
INNER JOIN
dbo.customer
ON
(
dbo.neighbourhood.id = dbo.customer.neighbourhoodId)
Group By dbo.province.provincename,
dbo.province.capital,
dbo.province.code,
dbo.province.telcode,
dbo.province.taxcode,
dbo.county.countyname,
dbo.district.districtname,
dbo.zone.alternateName,
dbo.neighbourhood.alternateName,
dbo.city.cityname,
dbo.city.taxcode,
dbo.city.fdocode
Below is my ds.xml
<DataSource ID="CusNeiGroupDS" serverType="sql">
<fields>
<field name="id" type="integer" />
<field name="provincename" title="province" type="text"/>
<field name="capital" title="capital" type="text"/>
<field name="code" title="code" type="text"/>
<field name="telcode" title="telcode" type="text"/>
<field name="taxcode" title="taxcode" type="text" />
<field name="provinceId" type="integer" tableName="county"/>
<field name="countyname" title="county" type="text" tableName="county"/>
<field name="district" title="district" type="text" />
<field name="city" title="city" type="text" />
<field name="zone" title="zone" type="text" />
<field name="neighbour" title="neighbour" type="text" />
<field name="taxcodecity" title="taxcodecity" type="text"
/>
<field name="fdocode" title="fdocode" type="text" />
<!-- <field name="countCustomer" title="countCustomer" type="int" /> -->
</fields>
<operationBindings>
<operationBinding operationId="summary"
operationType="fetch">
<selectClause>
districtname as
district,
alternateName as zone,
alternateName as neigbhour,
cityname
as city,
fdocode
</selectClause>
<tableClause>province, county, district, zone, neighbourhood, city
</tableClause>
<whereClause>
province.Id = county.provinceId
AND district.countyId = county.Id
AND city.districtId = district.Id
AND neighbourhood.zoneId = zone.Id
</whereClause>
</operationBinding>
</operationBindings>
the error I get is
Execute of select: SELECT COUNT(*) FROM CusNeiGroupDS WHERE ('1'='1')
on db: SQLServer threw exception: java.sql.SQLException: Invalid
object name 'CusNeiGroupDS'. - assuming stale connection and retrying
query.
But when I put the table name in the datasource like this,I get output but only from that table which I mention and not from the other tables which are joined with FK.
<DataSource ID="CusNeiGroupDS" serverType="sql" tableName="province">
I was able to achieve this with using includeFrom and foreignKey tags in the datasources. Then create another datasource where i use it to include all the coumns I need from the different tables.Like so
<DataSource ID="neighbourDS_1" serverType="sql" tableName="neighbourhood" inheritsFrom="neighbourDS">
<fields>
<field name="provincename" includeFrom="provinceDS.provincename" />
<field name="capital" includeFrom="provinceDS.capital" />
<field name="code" includeFrom="provinceDS.code" />
<field name="telcode" includeFrom="provinceDS.telcode" />
<field name="countyname" includeFrom="countyDS.countyname" />
<field name="district" includeFrom="districtDS.districtname" />
<field name="city" includeFrom="cityDS.cityname" />
<field name="taxcodecity" includeFrom="cityDS.taxcodecity" />
<field name="fdocode" includeFrom="cityDS.fdocode" />
<field name="zone" includeFrom="zoneDS.zone" />
</fields>
</DataSource>

SQL Server bulk insert XML format file

I am trying to load a fixed width text file using Bulk Insert and a XML format file. I have used the same process and XML file on another fixed width, except with less columns.
Error
Msg 4857, Level 16, State 1, Line 16
Line 4 in format file "\\PATHC\addr.xml": Attribute "type" could not be specified for this type.
SQL Server Table
create table [dbo].[raw_addr](
address_number varchar(max),
addr_linel varchar(max),
addr_line2 varchar(max),
street_no varchar(max),
street_name varchar(max),
street_type varchar(max),
locality varchar(max),
[state] varchar(max),
country varchar(max),
postcode varchar(max)
);
SQL Server Bulk Insert
BULK INSERT [dbo].[raw_addr] FROM '\\PATH\addr.txt'
WITH (
FORMATFILE = '\\PATH\addr.xml',
ROWTERMINATOR='\r\n');
XML Code:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema_instance">
<RECORD>
<FIELD ID="1" xsi:type="CharFixed" LENGTH="9" />
<FIELD ID="2" xsi:type="CharFixed" LENGTH="50" />
<FIELD ID="3" xsi:type="CharFixed" LENGTH="50" />
<FIELD ID="4" xsi:type="CharFixed" LENGTH="10" />
<FIELD ID="5" xsi:type="CharFixed" LENGTH="50" />
<FIELD ID="6" xsi:type="CharFixed" LENGTH="10" />
<FIELD ID="7" xsi:type="CharFixed" LENGTH="30" />
<FIELD ID="8" xsi:type="CharFixed" LENGTH="3" />
<FIELD ID="9" xsi:type="CharFixed" LENGTH="30" />
<FIELD ID="10" xsi:type="CharTerm" TERMINATOR="\r\n" LENGTH="4" />
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="address_number" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="2" NAME="addr_linel" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="3" NAME="addr_line2" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="4" NAME="street_no" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="5" NAME="street_name" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="6" NAME="street_type" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="7" NAME="locality" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="8" NAME="state" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="9" NAME="country" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="10" NAME="postcode" xsi:type="SQLNVARCHAR" />
</ROW>
</BCPFORMAT>
Example TXT extract
001044057C/- XXXXXX XXXXXXX 0000
001295978XXXX 0000
0013974311 0000
00124485712 0000
0012390352 0000
0014720345 0000
0014792876 0000
000986525 ARABANOO GRENFELL NSW 2810
000986589 PO BOX XX KEMPSEY NSW 2440
000740594 'RUSSLEY' ABERDEEN NSW 2336
000311516 BUNYARA ABERDEEN NSW 2336
000298796 CAMBRIA ABERDEEN NSW 2336
000540611 HALCOMBE ABERDEEN NSW 2336
000513112 'LARK HILL' ABERDEEN NSW 2336
000612955 FAN HILL ABERMAIN NSW 2326
001109439 BRAYWOOD ADAMINABY NSW 2630
000460864 TARA ADAMINABY NSW 2630
000315297 ADAMSTOWN NSW 2289
000470057 COORUMBENE ADELONG NSW 2729
000491941 NACKI ADELONG NSW 2729
I think your problem is in the SQL statement, try removing the ROWTERMINATOR. It is not necessary since you specify it in the format file.

How to get the xml node based on condition in sql server

This is my xml
DECLARE #XMLValues XML
SET #XMLValues = '<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENTS name="NYSPIT">
<DOCUMENT ID="140208512T200911101">
<REPEATS>
<REPEAT NAME="EXCEPTIONS">
<ROW>
<FIELD VALUE="09_NYC-3A_2" NAME="PageType"/>
<FIELD VALUE="" NAME="KeyWord"/>
<FIELD VALUE="020852009111001.002" NAME="ImageName"/>
<FIELD VALUE="2" NAME="PageNo"/>
<FIELD VALUE="" NAME="Qualifier"/>
</ROW>
</REPEAT>
</REPEATS>
</DOCUMENT>
<DOCUMENT ID="140208512T200911102">
<REPEATS>
<REPEAT NAME="EXCEPTIONS">
<ROW>
<FIELD VALUE="09_NYC-3A_2" NAME="PageType"/>
<FIELD VALUE="" NAME="KeyWord"/>
<FIELD VALUE="020852009111001.002" NAME="ImageName"/>
<FIELD VALUE="2" NAME="PageNo"/>
<FIELD VALUE="" NAME="Qualifier"/>
</ROW>
</REPEAT>
</REPEATS>
</DOCUMENT>
</DOCUMENTS>
and i need to retrieve the XML node for ID - 140208512T200911101 alone. i cant able to get the information using various methods, still didnt get the correct one.
my desired result should be like this :
<DOCUMENT ID="140208512T200911101">
<REPEATS>
<REPEAT NAME="EXCEPTIONS">
<ROW>
<FIELD VALUE="09_NYC-3A_2" NAME="PageType"/>
<FIELD VALUE="" NAME="KeyWord"/>
<FIELD VALUE="020852009111001.002" NAME="ImageName"/>
<FIELD VALUE="2" NAME="PageNo"/>
<FIELD VALUE="" NAME="Qualifier"/>
</ROW>
</REPEAT>
</REPEATS>
</DOCUMENT>
Please help on this...
Thanks for your support and it is working fine, for getting the #ID value dynamically from a variable we need to user like this :
DECLARE #DCN Varchar(50)
SET #DCN = '140208512T200911101'
select #XMLValues.query('/DOCUMENTS/DOCUMENT[#ID = sql:variable("#DCN")]')
select #XMLValues.query('/DOCUMENTS/DOCUMENT[#ID = "140208512T200911101"]')
Result
<DOCUMENT ID="140208512T200911101">
<REPEATS>
<REPEAT NAME="EXCEPTIONS">
<ROW>
<FIELD VALUE="09_NYC-3A_2" NAME="PageType" />
<FIELD VALUE="" NAME="KeyWord" />
<FIELD VALUE="020852009111001.002" NAME="ImageName" />
<FIELD VALUE="2" NAME="PageNo" />
<FIELD VALUE="" NAME="Qualifier" />
</ROW>
</REPEAT>
</REPEATS>
</DOCUMENT>

NHibernate Identity fields

Getting started with NHibernate
How can I generate identity fields in nHibernate using Hilo algorithm?
use class="hilo":
<generator class="hilo">
example:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate__MyClass" assembly="NHibernate__MyClass">
<class name="MyClass" table="MyClass">
<id name="Id" type="int" column="ID">
<generator class="hilo">
</id>
<property name="Name">
<column name="Name" not-null="true" />
</property>
<property name="Value">
<column name="Value" not-null="true" />
</property>
</class>
</hibernate-mapping>
I simplified:
<id name="Id">
<column name="ID" sql-type="int" not-null="true"/>
<generator class="hilo" />
</id>
to:
<id name="Id" type="int" column="ID">
<generator class="hilo">
</id>
You could have a syntax error of some sort that is confusing NHibernate.
If you could provide more detail about the code that is executing before the failure or anything else you might think is important, that could speed the rate at which your problem is resolved.
I haven't watched the screencasts yet. But Summer of nHibernate should help you.
I am sorry - I am not answering your original question.

Hibernate One-To-Many Unidirectional on an existing DB

Hello Stack Overflow Experts, i have need of your expertice:
I am trying to use Hibernate on an Existing DB.
Currently im trying to load a User object and a list of UserData objects that go along.
in the DB the (simplified) layout is
| User | | UserData |
---------------- -----------------------------------
uid | username | | uid | parentuid | field | value |
So each User object matches all the UserData objects where UserData.parentuid = User.uid.
My User class mapping file
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<!--<generator class="native"/>-->
</id>
<property name="username" column="username" />
<list name="fieldData" cascade="all">
<key column="parentuid" not-null="true" />
<index column="parentuid" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</list>
</class>
Mu UserData mapping file
<class name="com.agetor.commons.fields.FieldData" table="ac_userdef_data">
<id name="uid" column="uid" type="long" >
<!--<generator class="native"/> -->
</id>
<!--<property name="parentuid" column="parentuid" /> -->
<property name="fieldname" column="fieldname" />
<property name="value" column="value" />
</class>
So far i have tried many different configurations and all of them have had various degrees of failue. The code pasted here, does not work.
The parentuid property is commented out, because Hibernate gives a "Repeated column in mapping" error otherwise.
Currently there is still a "Repeated column in mapping" on the uid field, i use for <list-index />
I do not understand where i specify that UserData.parentuid is the foreign key and that the list should use User.uid as key.
I hope someone is able to help.
When you define both a One-To-Many and a Many-To-One, does this not make it Bi-Directional?
The current working model, is Unidirectional and UserData does not have a reference to User. Your suggestion fails, because Hibernate could not find a get or set method for User on UserData.
Is it implied that, this code uses User.uid as a key and matches this against the UserData.parentuid column? Or is this fact specified somewhere else?
<list name="fieldData" inverse="true">
<key column="parentuid" not-null="true" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</list>
I am still learning Hibernate and working my way through documentation and examples i can find.
Try this (drycode):
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<!--<generator class="native"/>-->
</id>
<property name="username" column="username" />
<list name="fieldData" inverse="true">
<key column="parentuid" not-null="true" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</list>
</class>
<class name="com.agetor.commons.fields.FieldData" table="ac_userdef_data">
<id name="uid" column="uid" type="long" >
<!--<generator class="native"/> -->
</id>
<many-to-one name="user" class="com.agetor.commons.security.User" fetch="join">
<column name="parentuid" not-null="true"/>
</many-to-one>
<property name="fieldname" column="fieldname" />
<property name="value" column="value" />
</class>
If it works, try adding the index and cascade stuff that I left out.
cheers,
mitch
I think you're close, but List is probably not the data structure you want in your new User class. A list implies an ordering of the child elements, which the tables don't seem to have. If you have an indexed collection like a list, the data has to have a separate column that provides the index, hence the "repeated column in mapping" issue.
The rules of thumb I use are - each mapping should include an entry for each member of the class it is mapping. So for instance your User class should have an entry for its collection of FieldData, but your FieldData mapping does not need an entry for parentuid, since that column doesn't map to an element of the FieldData class - it's just used to put the FieldData within the collection in the parent object.
If the User object really does contain an ordered collection of FieldData, then use a set instead. On the other hand, I think it's more likely you'd want a map of FieldData instead, using the 'field' name as the index. Then the FieldData class doesn't need to map the 'field' column and won't have that as a member, it'll just have the value (and any other fields from this table - of course if there really is only one column left to map, you might just end up with a map of strings to strings.
This is the latest development on this problem. This configuration can succesfully load from the Database. Saving does not work.
I have decided to alter the Database design instead, so im posting this here for reference for others, before i abandon this approach.
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<generator class="increment"/>
</id>
<property name="deleted" column="deleted" />
<property name="username" column="username" />
<property name="password" column="passwd" />
<property name="disabled" column="disabled" />
<property name="lockout" column="lockout" />
<property name="expires" column="expires" />
<bag name="fieldData" lazy="extra">
<key column="parentuid" not-null="true" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</bag>
<bag name="groups" table="ac_group_rel" access="field" lazy="extra">
<key column="useruid"/>
<many-to-many column="groupuid" class="com.agetor.commons.security.Group"/>
</bag>
<join table="ac_userdef_data" optional="true" fetch="join">
<subselect>
select
*
from
ac_userdef_data data
where
data.objectname = 'user' and
data.fieldname = 'firstname'
</subselect>
<key column="parentuid" />
<property name="firstname" formula="(select data.value from ac_userdef_data data where data.fieldname = 'firstname' and data.uid = uid)"/>
</join>
<join table="ac_userdef_data" optional="true" fetch="join">
<subselect>
select
*
from
ac_userdef_data data
where
data.objectname = 'user' and
data.fieldname = 'lastname'
</subselect>
<key column="parentuid" />
<property name="lastname" formula="(select data.value from ac_userdef_data data where data.fieldname = 'lastname' and data.uid = uid)"/>
</join>
</class>
-->
<list name="fieldData" cascade="all">
<key column="uid" not-null="true" />
<index column="parentuid" />
<one-to-many class="com.agetor.commons.fields.FieldData"/>
</list>
Try this , i just changed key coloumn as parentuid to uid.(And better use set in hibernate mappings)
cheers,
Nagendra Prasad..

Resources