These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Foreign Key wird nicht mitgespeichert
PostPosted: Mon Jan 08, 2007 6:37 am 
Newbie

Joined: Mon Jan 08, 2007 6:20 am
Posts: 3
Mir stellt sich folgendes (hoffentlich banales Problem):
Ich habe in MySQL folgende Table:

CREATE TABLE `wsx_hib`.`wsx_group` (
`group_id` int(11) unsigned NOT NULL,
`org_id` int(11) unsigned NOT NULL,
`group_name` varchar(30) NOT NULL,
`lang_id` int(11) unsigned NOT NULL,
`lang_fixed` smallint(5) unsigned NOT NULL COMMENT 'Sprache fix eingestellt?',
`role_id` int(11) unsigned NOT NULL,
`group_system` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`group_id`,`org_id`),
KEY `FK9DFE19C255F7A78` (`org_id`),
KEY `FK9DFE19C5C6FE781` (`lang_id`),
KEY `FK9DFE19C757FA2D7` (`role_id`,`org_id`),
CONSTRAINT `groupLang` FOREIGN KEY (`lang_id`) REFERENCES `wsx_lang` (`lang_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `groupOrg1` FOREIGN KEY (`org_id`) REFERENCES `wsx_organization` (`org_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `groupRole` FOREIGN KEY (`role_id`, `org_id`) REFERENCES `wsx_role` (`role_id`, `org_id`) ON DELETE CASCADE ON UPDATE CASCADE
)

Wenn ich ein Objekt dieser Instanz speichern will dann wird der FK nicht gesetzt und es entsteht eine Exception (da für den FK-Wert NOT NULL gesetzt wurde)
Wie man aus der SQL-Ausgabe entnehmen kann wird der FK-Werte (role_id) nicht gesetzt
Wie man dem Code weiter unten entnehmen kann, wird der Werte allerdings gesetzt

Der Werte der Rolle (WsxRole r) ist nicht null ( ;-) )

Hibernate version: 3.2.0.beta8

Mapping documents:

hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">true</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">1234567</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/wsx_hib</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<mapping resource="tables/WsxFormQueue.hbm.xml" />
<mapping resource="tables/WsxLog.hbm.xml" />
<mapping resource="tables/WsxFormGrouping.hbm.xml" />
<mapping resource="tables/WsxGroup.hbm.xml" />
<mapping resource="tables/WsxFormLang.hbm.xml" />
<mapping resource="tables/WsxUser.hbm.xml" />
<mapping resource="tables/WsxUserSession.hbm.xml" />
<mapping resource="tables/WsxLang.hbm.xml" />
<mapping resource="tables/WsxFiletype.hbm.xml" />
<mapping resource="tables/WsxFormTitle.hbm.xml" />
<mapping resource="tables/WsxMsgLang.hbm.xml" />
<mapping resource="tables/WsxConfig.hbm.xml" />
<mapping resource="tables/WsxForm.hbm.xml" />
<mapping resource="tables/WsxDir.hbm.xml" />
<mapping resource="tables/WsxMsgGroup.hbm.xml" />
<mapping resource="tables/WsxRolePermission.hbm.xml" />
<mapping resource="tables/WsxFormGroupingLang.hbm.xml" />
<mapping resource="tables/WsxFormGroupVisible.hbm.xml" />
<mapping resource="tables/WsxFormHelp.hbm.xml" />
<mapping resource="tables/WsxOrganization.hbm.xml" />
<mapping resource="tables/WsxClient.hbm.xml" />
<mapping resource="tables/WsxPermission.hbm.xml" />
<mapping resource="tables/WsxFormClient.hbm.xml" />
<mapping resource="tables/WsxOrganizationUrl.hbm.xml" />
<mapping resource="tables/WsxQuery.hbm.xml" />
<mapping resource="tables/WsxFormSession.hbm.xml" />
<mapping resource="tables/WsxGroupMember.hbm.xml" />
<mapping resource="tables/WsxMsg.hbm.xml" />
<mapping resource="tables/WsxDirOpen.hbm.xml" />
<mapping resource="tables/WsxQueue.hbm.xml" />
<mapping resource="tables/WsxRole.hbm.xml" />
</session-factory>
</hibernate-configuration>

WsxGroup.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 08.01.2007 10:25:20 by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="at.invaris.wsx.data.table.WsxGroup" table="wsx_group" catalog="wsx_hib">
<comment></comment>
<composite-id name="id" class="at.invaris.wsx.data.table.WsxGroupId">
<key-property name="groupId" type="int">
<column name="group_id" />
</key-property>
<key-property name="orgId" type="int">
<column name="org_id" />
</key-property>
</composite-id>
<many-to-one name="wsxOrganization" class="at.invaris.wsx.data.table.WsxOrganization" update="false" insert="false" fetch="select">
<column name="org_id" not-null="true">
<comment></comment>
</column>
</many-to-one>
<many-to-one name="wsxRole" class="at.invaris.wsx.data.table.WsxRole" update="false" insert="false" fetch="select">
<column name="role_id" not-null="true">
<comment></comment>
</column>
<column name="org_id" not-null="true">
<comment></comment>
</column>
</many-to-one>
<many-to-one name="wsxLang" class="at.invaris.wsx.data.table.WsxLang" fetch="select">
<column name="lang_id" not-null="true">
<comment></comment>
</column>
</many-to-one>
<property name="groupName" type="string">
<column name="group_name" length="30" not-null="true">
<comment></comment>
</column>
</property>
<property name="langFixed" type="short">
<column name="lang_fixed" not-null="true">
<comment>Sprache fix eingestellt?</comment>
</column>
</property>
<property name="groupSystem" type="short">
<column name="group_system" not-null="true">
<comment></comment>
</column>
</property>
<set name="wsxFormGroupVisibles" inverse="true">
<key>
<column name="org_id" not-null="true">
<comment></comment>
</column>
<column name="group_id" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="at.invaris.wsx.data.table.WsxFormGroupVisible" />
</set>
<set name="wsxGroupMembers" inverse="true">
<key>
<column name="org_id" not-null="true">
<comment></comment>
</column>
<column name="group_id" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="at.invaris.wsx.data.table.WsxGroupMember" />
</set>
<set name="wsxFormSessions" inverse="true">
<key>
<column name="group_id_to">
<comment></comment>
</column>
<column name="org_id" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="at.invaris.wsx.data.table.WsxFormSession" />
</set>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

WsxRole r = getRole(org_id, role_id);

WsxGroupId gid = new WsxGroupId();
gid.setOrgId(org_id);
WsxGroup g = new WsxGroup();
g.setId(gid);
g.setWsxLang(l);
g.setGroupName(group_name);
g.setLangFixed( (short) lang_typ);
g.setWsxRole(r);

persist(g);

Full stack trace of any exception that occurs:

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1562)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at at.invaris.wsx.data.dbcon.getGroups(dbcon.java:189)
at at.invaris.wsx.www.settings.settings_organizations_show.get(settings_organizations_show.java:1873)
at at.invaris.wsx.www.settings.settings_main_show.get(settings_main_show.java:73)
at at.invaris.wsx.requests.WSXRequest.includePageRequest(WSXRequest.java:464)
at at.invaris.wsx.requests.WSXRequest.handlePageRequest(WSXRequest.java:331)
at at.invaris.wsx.requests.WSXRequest.handleRequest(WSXRequest.java:110)
at at.invaris.wsx.servlets.WSXServlet.handleRequest(WSXServlet.java:350)
at at.invaris.wsx.servlets.WSXServlet.doPost(WSXServlet.java:378)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.BatchUpdateException: Field 'role_id' doesn't have a default value
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:657)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 31 more


Name and version of the database you are using:

MySQL 5.0 Community Edition

The generated SQL (show_sql=true):

insert into wsx_hib.wsx_group (lang_id, group_name, lang_fixed, group_system, group_id, org_id) values (?, ?, ?, ?, ?, ?)


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 7:06 am 
Newbie

Joined: Mon Jan 08, 2007 6:20 am
Posts: 3
ich bekomme das problem auch in anderen tables, immer dann wenn ich das objekt setze auf das referenziert werden soll dan wird in die db nur null gespeichert obwohl der wert nicht null ist


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 9:51 am 
Newbie

Joined: Mon Jan 08, 2007 6:20 am
Posts: 3
das attribute insert="false" bewirkt dass man das setzen des fk von der anderen seite (also dem dem set auf der 1-seite) angehen muss

so steht es is der doku:

Parent p = (Parent) sess.load(Parent.class, id);
Child c = new Child();
c.setParent(p);
p.getChildren().add(c); //no need to fetch the collection!
sess.flush();

das ändert aber nichts an dem problem, der fk wird nicht gesetzt
den wert des attributes insert kann ich allerdings nicht auf true setzen weil sonst eine exception kommt weil der feld-identifier in mehreren constraints vorkommt

dieses verhalten wurde in einigen früheren threads scheinbar als bug abgestempelt (für die 3.2.1 ga)
ich hoffe dass jemand vielleicht schon ein workaround dafür hat


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.