-->
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: Unidirectional ManyToMany Mapping
PostPosted: Tue Nov 06, 2007 12:06 pm 
Newbie

Joined: Tue Nov 06, 2007 11:29 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2

Mapping documents:
Online Documentation and Pro Hibernate 3 book

Code between sessionFactory.openSession() and session.close():
session.beginTransaction();
message.setCreateUserId("system");
session.save(message);
session.getTransaction().commit();

Full stack trace of any exception that occurs:
org.hibernate.exception.DataException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:77)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2312)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2257)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2557)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at net.greenorchid.discussion.dataaccess.MessageHibernateDao.create(MessageHibernateDao.java:33)
at test.unit.net.greenorchid.discussion.dataaccess.MessageHibernateDaoTest.testCreate(MessageHibernateDaoTest.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.BatchUpdateException: null, message from server: "Column was set to data type implicit default; NULL supplied for NOT NULL column 'CRETD_DTTM' at row 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1469)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 32 more


Name and version of the database you are using:
MySql - Version 5.2

The generated SQL (show_sql=true):
update
data_resource
set
cretd_dttm=?,
cretd_by_db_user_nme=?,
cretd_by_user_nme=?,
cretd_by_user_role=?,
from_dttm=?,
last_mofd_dttm=?,
mofd_by_user_nme=?,
mofd_by_user_role=?,
mofd_by_db_user_nme=?,
thru_dttm=?,
resource_qualifier=?
where
resource_id=?
Hibernate:
update
data_resource
set
cretd_dttm=?,
cretd_by_db_user_nme=?,
cretd_by_user_nme=?,
cretd_by_user_role=?,
from_dttm=?,
last_mofd_dttm=?,
mofd_by_user_nme=?,
mofd_by_user_role=?,
mofd_by_db_user_nme=?,
thru_dttm=?,
resource_qualifier=?
where
resource_id=?

Problems with Session and transaction handling? no

Read this: http://hibernate.org/42.html


Attempting to map a unidirectional manytomany mapping between two classes - Message and Topic as follows. An attempt is made to perform an update statement on the message table that does not occur when topics is made transient or CascadeType.PERIST is set. Please advice on the correct way to create a unidirectional manytomany mapping

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "message_topic",
joinColumns = { @JoinColumn(name = "message_id", table
="message")},
inverseJoinColumns = {@JoinColumn(name = "topic_id", table ="topic")}
)
public List<Topic> getTopics() {
return topics;
}


Class
@Entity
@PrimaryKeyJoinColumn(name = "message_id")
@Table(name = "message")
@DiscriminatorValue("M")
Message

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "message_topic",
joinColumns = { @JoinColumn(name = "message_id", table
="message")},
inverseJoinColumns = {@JoinColumn(name = "topic_id", table ="topic")}
)
public List<Topic> getTopics() {
return topics;
}

@Entity
@PrimaryKeyJoinColumn(name = "topic_id")
@Table(name = "topic")
@DiscriminatorValue("T")
Topic
String name

Table
Message (message_id - pk)
Topic (topic_id - pk)
message_topic( relationship table - message_id and topic_id)


Debug level Hibernate log excerpt:

08:47:16,484 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
08:47:16,484 DEBUG JDBCTransaction:103 - commit
08:47:16,484 DEBUG AbstractFlushingEventListener:111 - processing flush-time cascades
08:47:16,484 DEBUG AbstractFlushingEventListener:154 - dirty checking collections
08:47:16,484 DEBUG Collections:176 - Collection found: [net.greenorchid.discussion.Message.topics#201], was: [<unreferenced>] (initialized)
08:47:16,500 DEBUG AbstractFlushingEventListener:85 - Flushed: 1 insertions, 1 updates, 0 deletions to 2 objects
08:47:16,500 DEBUG AbstractFlushingEventListener:91 - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
08:47:16,500 DEBUG Printer:83 - listing entities:
08:47:16,500 DEBUG Printer:90 - net.greenorchid.discussion.Topic{createUserId=null, thruDate=null, modifiedUserId=null, fromDate=null, createdByUserId=null, modifiedByUserRole=null, createdByUserRole=null, modifiedByUserId=null, createTime=null, identifier=3, qualifier=null, lastModifiedTime=null, name=null}
08:47:16,515 DEBUG Printer:90 - net.greenorchid.discussion.Message{createUserId=system, title=Allowing case email attachment, thruDate=null, modifiedUserId=null, fromDate=null, messageText=Allowing case email attachment, createdByUserId=createdByUserId, modifiedByUserRole=null, createdByUserRole=createdByUserRole, modifiedByUserId=null, createTime=2007-11-06 08:47:16, identifier=201, qualifier=TEST, lastModifiedTime=null, topics=[net.greenorchid.discussion.Topic#3]}
08:47:16,515 DEBUG AbstractBatcher:358 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
08:47:16,515 DEBUG SQL:393 -
insert
into
data_resource
(cretd_dttm, cretd_by_db_user_nme, cretd_by_user_nme, cretd_by_user_role, from_dttm, last_mofd_dttm, mofd_by_user_nme, mofd_by_user_role, mofd_by_db_user_nme, thru_dttm, resource_qualifier, resource_id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
data_resource
(cretd_dttm, cretd_by_db_user_nme, cretd_by_user_nme, cretd_by_user_role, from_dttm, last_mofd_dttm, mofd_by_user_nme, mofd_by_user_role, mofd_by_db_user_nme, thru_dttm, resource_qualifier, resource_id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
08:47:16,531 DEBUG AbstractBatcher:44 - Executing batch size: 1
08:47:16,531 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
08:47:16,531 DEBUG AbstractBatcher:358 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
08:47:16,531 DEBUG SQL:393 -
insert
into
message
(message_text, message_title, message_id)
values
(?, ?, ?)
Hibernate:
insert
into
message
(message_text, message_title, message_id)
values
(?, ?, ?)
08:47:16,531 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
08:47:16,531 DEBUG AbstractBatcher:358 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
08:47:16,531 DEBUG SQL:393 -
update
data_resource
set
cretd_dttm=?,
cretd_by_db_user_nme=?,
cretd_by_user_nme=?,
cretd_by_user_role=?,
from_dttm=?,
last_mofd_dttm=?,
mofd_by_user_nme=?,
mofd_by_user_role=?,
mofd_by_db_user_nme=?,
thru_dttm=?,
resource_qualifier=?
where
resource_id=?
Hibernate:
update
data_resource
set
cretd_dttm=?,
cretd_by_db_user_nme=?,
cretd_by_user_nme=?,
cretd_by_user_role=?,
from_dttm=?,
last_mofd_dttm=?,
mofd_by_user_nme=?,
mofd_by_user_role=?,
mofd_by_db_user_nme=?,
thru_dttm=?,
resource_qualifier=?
where
resource_id=?
08:47:16,546 DEBUG AbstractBatcher:44 - Executing batch size: 1
08:47:16,546 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
08:47:16,546 DEBUG JDBCExceptionReporter:69 - Could not execute JDBC batch update [update data_resource set cretd_dttm=?, cretd_by_db_user_nme=?, cretd_by_user_nme=?, cretd_by_user_role=?, from_dttm=?, last_mofd_dttm=?, mofd_by_user_nme=?, mofd_by_user_role=?, mofd_by_db_user_nme=?, thru_dttm=?, resource_qualifier=? where resource_id=?]
java.sql.BatchUpdateException: null, message from server: "Column was set to data type implicit default; NULL supplied for NOT NULL column 'CRETD_DTTM' at row 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1469)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2312)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2257)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2557)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at net.greenorchid.discussion.dataaccess.MessageHibernateDao.create(MessageHibernateDao.java:33)
at test.unit.net.greenorchid.discussion.dataaccess.MessageHibernateDaoTest.testCreate(MessageHibernateDaoTest.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
08:47:16,546 WARN JDBCExceptionReporter:77 - SQL Error: 1263, SQLState: 22004
08:47:16,546 ERROR JDBCExceptionReporter:78 - null, message from server: "Column was set to data type implicit default; NULL supplied for NOT NULL column 'CRETD_DTTM' at row 1"
08:47:16,562 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.DataException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:77)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2312)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2257)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2557)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at net.greenorchid.discussion.dataaccess.MessageHibernateDao.create(MessageHibernateDao.java:33)
at test.unit.net.greenorchid.discussion.dataaccess.MessageHibernateDaoTest.testCreate(MessageHibernateDaoTest.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.BatchUpdateException: null, message from server: "Column was set to data type implicit default; NULL supplied for NOT NULL column 'CRETD_DTTM' at row 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1469)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 32 more
08:47:16,562 DEBUG SessionImpl:220 - opened session at timestamp: 4892100348157952
08:47:16,562 DEBUG JDBCTransaction:54 - begin
08:47:16,562 DEBUG ConnectionManager:415 - opening JDBC connection
08:47:16,562 DEBUG DriverManagerConnectionProvider:109 - opening new JDBC connection
08:47:16,578 DEBUG DriverManagerConnectionProvider:115 - created connection to: jdbc:mysql://localhost/greenorchid, Isolation Level: 4
08:47:16,578 DEBUG JDBCTransaction:59 - current autocommit status: false
08:47:16,578 DEBUG AbstractBatcher:358 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
08:47:16,578 DEBUG SQL:393 -


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 10:39 pm 
Newbie

Joined: Wed Jan 11, 2006 9:02 am
Posts: 11
Hi,

Try to use this sample to map an bidirectional association many-to-many between Message and Topic.

Code:
public class Message {
    @Id
    @Column(name = "message_id", nullable = false)
    @GeneratedValue( strategy=GenerationType.AUTO )
    private Long idMessage;

    @ManyToMany(
        fetch=FetchType.EAGER,
        targetEntity = Topic.class,
        cascade={CascadeType.REMOVE, CascadeType.MERGE, CascadeType.REFRESH}
    )
    @JoinTable(
        name="message_topic",
        joinColumns={@JoinColumn(name="message_id")},
        inverseJoinColumns={ @JoinColumn( name="topic_id" )},
        uniqueConstraints={ @UniqueConstraint(columnNames={ "message_id", "topic_id"})}
    )
    private Set<Topic> topics = new HashSet<Topic>();

    // Accessors methods hidden
}

public class Topic {
    @Id
    @Column(name = "topĂ­c_id", nullable = false)
    @GeneratedValue( strategy=GenerationType.AUTO )
    private Long idTopic;

    @ManyToMany(
        cascade={CascadeType.PERSIST, CascadeType.MERGE},
        mappedBy="topics",
        targetEntity=Message.class
    )
    private Set<Message> messages;

    // Accessors methods hidden
}


If you want an unidirectional association, just remove many-to-may annotation and set variavel messages from Topic.

When you use source code in the message, please put in it between [ code] ... [/ code] tags.


Regards,
Cleiton


Top
 Profile  
 
 Post subject: Unidirectional ManyToMany Mapping
PostPosted: Wed Nov 07, 2007 5:46 am 
Newbie

Joined: Tue Nov 06, 2007 11:29 am
Posts: 2
That worked

Thank You


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.