You do not need to set the dynamic model:
Session = sessionfactory. opensession ();
Session = session. getsession (entitymode. Map );
The session can be automatically determined.
The persistence entity in the runtime does not need to be expressed as a pojo class or a JavaBean object. Hibernate also supports dynamic models (used during runtime)MapOfMap) And the entity representation like the dom4j tree model. In this way, you don't need to write persistence classes, just write the ing file.
Hibernate works in normal pojo mode by default. You can use configuration optionsDefault_entity_mode, For specificSessionfactoryTo set a default Object Representation mode. (See hibernate configuration attributes.
"Href =" http://writeblog.csdn.net/ch03s04.html#configuration-optional-properties ">Table 3.3 "hibernate configuration attributes".)
UseMap. First, in the ing file, declareEntity-nameTo replace a class name (or as a subsidiary ).
<Hibernate-mapping> <class entity-name = "customer"> <ID name = "ID" type = "long" column = "ID"> <generator class = "sequence" /> </ID> <property name = "name" column = "name" type = "string"/> <property name = "Address" column = "Address" type =" string "/> <your-to-one name =" Organization "column =" organization_id "class =" Organization "/> <bag name =" orders "inverse =" true "lazy = "false" cascade = "all"> <key column = "customer_id"/> <one-to-define class = "order"/> </bag> </class>
Note: although the target class name is used to declare the association, the associated target type can be a dynamic entity in addition to pojo.
In useDynamic-MapIsSessionfactoryAfter the default object mode is set, it can be used at runtime.MapOfMap.
Session S = opensession (); transaction Tx = S. begintransaction (); Session S = opensession (); // create a customermap David = new hashmap (); David. put ("name", "David"); // create an organizationmap foobar = new hashmap (); foobar. put ("name", "foobar Inc. "); // link bothdavid. put ("Organization", foobar); // save boths. save ("customer", David); S. save ("Organization", foobar); Tx. commit (); S. close ();
The advantage of dynamic ing is that it takes less time to change because the prototype does not need to implement entity classes. However, you cannot perform type checks during the compilation period and may handle many runtime exceptions. Thanks to the hibernate ing, it makes the schema of the data base easy to normalize and rationalize, and allows you to add appropriate domain model implementation on it later.
The entity representation mode can also be used in eachSessionBased on settings:
Session dynamicsession = pojoseph ssion. getsession (entitymode. map); // create a customermap David = new hashmap (); David. put ("name", "David"); dynamicsession. save ("customer", David );... dynamicsession. flush (); dynamicsession. close ()... // continue on pojoseph ssion
Note thatEntitymodeCallGetsession ()YesSessionIn the API, insteadSessionfactory. In this way, the newSessionShares underlying JDBC connections, transactions, and other context messages. This means that you do not needSessionCallingFlush ()AndClose ()Similarly, the transaction and connection processing are handed over to the original unit of work.