Hibernate version: 3.1.2
The Hibernate on-line reference says:
Quote:
update, insert (optional - defaults to true) : specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" property whose value is initialized from some other property that maps to the same colum(s) or by a trigger or other application.
Mapping documents:Snippet from the Message ( parent ) mapping file:
Code:
<id name="messageId" type="integer">
<meta attribute="scope-set">protected</meta>
<meta attribute="use-in-equals">true</meta>
<column name="message_id" />
<generator class="native" />
</id>
<set name="notificationHistories" lazy="true" inverse="true" order-by="notified_on desc">
<key>
<column name="message_id" not-null="true" />
</key>
<one-to-many class="com.mig.connectivity.hibernate.MessageNotificationHistory" />
</set>
Snippet from the MessageNotificationHistory ( child ) mapping file:
Code:
<property name="messageId" type="integer">
<column name="message_id" not-null="true" />
</property>
<many-to-one name="message" lazy="no-proxy" class="com.mig.connectivity.hibernate.Message" insert="false" update="false">
<column name="message_id" not-null="true" />
</many-to-one>
The reason that I want to do this is that ... there are several cases where I already have the id for the foreign key ( obtained elsewhere ... from a legacy system, properties file, or other non-Hibernate means ).
Thus, I don't want to have Hibernate load the parent entity Message every time a new child entity MessageNotificationHistory is being persisted to the database. This means that I don't want Hibernate to do a SELECT on the parent table every time I do an INSERT into the child table. However, that is what Hibernate currently does. There will be times that after loading the child entity, I then want to tell Hibernate to load the parent entity lazily.
So, I tried to map the column twice ... once using a many-to-one to the parent entity, and the second as a normal property as an integer. The on-line reference says this is possible:
Quote:
Setting both to false allows a pure "derived" property whose value is initialized from some other property that maps to the same colum(s)
However, what I get is the stack trace below:
Full stack trace of any exception that occurs:Code:
org.hibernate.MappingException: Repeated column in mapping for entity: com.mig.connectivity.hibernate.MessageNotificationHistory column: message_id (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:575)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:597)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:615)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:405)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:984)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1169)
at com.mig.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:32)
at softgame.gateway.connectors.smpp.AbstractDeliveryReceiptConsumer.saveSubmitResp(AbstractDeliveryReceiptConsumer.java:408)
at softgame.gateway.connectors.smpp.DeliveryReceiptJMSConsumerImpl.run(DeliveryReceiptJMSConsumerImpl.java:266)
at java.lang.Thread.run(Thread.java:595)