Question and answer in Hibernate

Embed Size (px)

Citation preview

  • 8/16/2019 Question and answer in Hibernate

    1/12

    Q: What is Hibernate?

    A: Hibernate is a java-based Object/relational mapping(ORM) tool.

    Q: Wat are ORM tools!A: ORM tools provide a"tomated sol"tions #or te Object/relational paradigmmismatc problem$ "sing metadata tat describes te mapping bet%een teobjects and te database.

    Q: Wat is Object/relational paradigm mismatc!A: Object-Oriented paradigm is based on so#t%are engineering principles$%ereas relational paradigm on matematical principles. Object-orientedtecnolog& s"pports te b"ilding o# applications o"t o# net%or's o# objects%it bot data and beavior. Relational tecnolog& s"pports te storage o#data in tables and manip"lation o# tat data "sing data manip"lationlang"age (M). *eca"se te "nderl&ing paradigms are di##erent te t%otecnologies do not %or' seamlessl&$ ence te name Object/relationalparadigm mismatc.

    Q: Wat role does te +ession inter#ace pla& in Hibernate!

    A: ,e +ession is a persistence manager tat manages operation li'e storingand retrieving objects. nstances o# +ession are inepensive to create anddestro&. ,e& are not treadsa#e.

    Q: Wat is +essionactor& inter#ace!A: ,e application obtains +ession instances #rom a +essionactor&.+essionactor& instances are not ligt%eigt and t&picall& one instance iscreated #or te %ole application. # te application accesses m"ltipledatabases$ it needs one per database

    Q: Wat is 0on#ig"ration inter#ace!

    A: ,e application "ses a 0on#ig"ration instance to speci#& te location o#mapping doc"ments and Hibernate-speci#ic properties and ten creates te+essionactor&.

    Q: Wat is te naming convention #or Hibernate 1M mapping #ile etensions!A: .bm.ml

    Q: Wat are te most common metods o# con#ig"ring Hibernate!A: 2. *& placing ibernate.properties #ile in te classpat.3. ncl"ding 4propert&5 elements in ibernate.c#g.ml in te classpat.

    Q: Ho% can te mapping #iles be con#ig"red in Hibernate!A: 2. Mapping #iles can be added to 0on#ig"ration in te application codeor$3. ,e& can be con#ig"red in ibernate.c#g.ml "sing te 4mapping5elements.

    Q: Wat appens %en bot ibernate.properties and ibernate.c#g.ml are inte classpat!A: ,e settings o# te 1M con#ig"ration #ile %ill override te settings"sed in te properties.

  • 8/16/2019 Question and answer in Hibernate

    2/12

    Q: +ince +essionactor& instances are not ligt%eigt$ %ere is te singlecreated instance placed in 6377 environments!A: 8s"all& it is bo"nd to 69$ it %ill bind itsel# a"tomaticall& i#ibernate.session#actor&name is set to te name o# director& node.

    Q: Ho% to set Hibernate to log all generated +Q to te console!A: *& setting te ibernate.so%s;l propert& to tr"e.

    Q: n ibernate$ %at inter#aces/classes m"st te persistent classes(classes tat are mapped to database tables) implement/etend!A: 9O97$ te& can be reg"lar

  • 8/16/2019 Question and answer in Hibernate

    3/12

    epression can be de#ined "sing te #orm"la attrib"te o# te 4propert&5element.

    Q: Ho% can &o" ma'e a propert& be read #rom te database b"t not modi#iedin an&%a& (ma'e it imm"table)!A: *& "sing te insert?@#alse@ and "pdate?@#alse@ attrib"tes.

    Q: Ho% can a %ole class be mapped as imm"table!

    A: *& "sing te m"table?@#alse@ attrib"te in te class mapping.

    Q: Wat is te "se o# d&namic-insert and d&namic-"pdate attrib"tes in aclass mapping!A: ,e& tell ibernate %eter to incl"de "nmodi#ied properties in +Q9+7R, and +Q 8

  • 8/16/2019 Question and answer in Hibernate

    4/12

    !onnection close()

    (nd the session by releasing the )*# connection and cleaning up.

    +

    !riteria create!riteria(!lass persistent!lass)

    #reate a new #riteria instance, for the given entity class, or a superclass of an entity class.

    !riteria create!riteria(String entityNa"e)

    #reate a new #riteria instance, for the given entity name.

    -

    Seriali#ablegetIdentifier($b%ect ob%ect)

    eturn the identifier value of the given entity as associated with this session.

    /

    Qery createFilter($b%ect collection& String 'eryString)

    #reate a new instance of 0uery for the given collection and filter string.

    Qery createQery(String 'eryString)

    #reate a new instance of 0uery for the given H02 %uery string.

    13

    SQQerycreateSQQery(String 'eryString)

    #reate a new instance of 4020uery for the given 402 %uery string.

    11

    void delete($b%ect ob%ect)

    emove a persistent instance from the datastore.

    1"

    void delete(String entityNa"e& $b%ect ob%ect)

    emove a persistent instance from the datastore.

    1&

    Session get(String entityNa"e& Seriali#able id)

    eturn the persistent instance of the given named entity with the given identifier, or null if there is nosuch persistent instance.

    1

    SessionFactorygetSessionFactory()

    5et the session factory which created this session.

    1+ void refresh($b%ect ob%ect)

  • 8/16/2019 Question and answer in Hibernate

    5/12

    e6read the state of the given instance from the underlying database.

    1

    Transaction getTransaction()

    5et the Transaction instance associated with this session.

    1-

    booleanis!onnected()

    #heck if the session is currently connected.

    1/

    booleanisDirty()

    *oes this session contain any changes which must be synchroni7ed with the database8

    1

    booleanis$pen()

    #heck if the session is still open.

    "3

    Seriali#able save($b%ect ob%ect)

    9ersist the given transient instance, first assigning a generated identifier.

    "1

    void save$rpdate($b%ect ob%ect)

    (ither save:;b!ect< or update:;b!ect< the given instance.

    ""

    void pdate($b%ect ob%ect)

    =pdate the persistent instance with the identifier of the given detached instance.

    "&

    void pdate(String entityNa"e& $b%ect ob%ect)

    =pdate the persistent instance with the identifier of the given detached instance.

    Property name Purpose

    hibernate.connection.driver_class JDBC driver class

    hibernate.connection.url JDBC URLhibernate.connection.username database user 

    hibernate.connection.password database user password 

    hibernate.connection.pool_size maximum number of pooled connections

    roperty name Purpose

    hibernate.dialect The classname of a Hibernate org.hibernate.dialect.Dialect whichallows Hibernate to generate SQL optimized for a particular

  • 8/16/2019 Question and answer in Hibernate

    6/12

    roperty name Purpose

    relational database.

    e.g.full.classname.of.Dialect

    In most cases Hibernate will actuall be able to choose thecorrect org.hibernate.dialect.Dialect implementation based on the

    !D"# metadata returned b the !D"# driver.

    hibernate.show_s$l

    %rite all SQL statements to console. This is an alternative tosetting the log categor org.hibernate.SQL to debug.

    e.g.true & false

    hibernate.format_s$l

    'rett print the SQL in the log and console.

    e.g.true & false

    hibernate.default_schema

    Qualif un$ualified table names with the givenschema(tablespace in generated SQL.

    e.g.S#H)*+_,+*)

    hibernate.default_catalog

    Qualifies un$ualified table names with the given catalog ingenerated SQL.

    e.g.#+T+L-_,+*)

    hibernate.session_factor_name

    The org.hibernate.Session/actor will be automaticall bound tothis name in !,DI after it has been created.

    e.g. 0ndi(composite(name

    hibernate.ma1_fetch_depth

    Sets a ma1imum 2depth2 for the outer 0oin fetch tree for single3ended associations 4one3to3one5 man3to3one6. + 7 disablesdefault outer 0oin fetching.

    e.g. recommended values between 7 and 8

    hibernate.default_batch_fetch_size

    Sets a default size for Hibernate batch fetching of associations.

    e.g. recommended values 95 :5 ;<

    hibernate.default_entit_mode

    Sets a default mode for entit representation for all sessionsopened from this Session/actor

    dnamic3map5 dom905 po0o

  • 8/16/2019 Question and answer in Hibernate

    7/12

    roperty name Purpose

    hibernate.order_updates

    /orces Hibernate to order SQL updates b the primar =e valueof the items being updated. This will result in fewer transactiondeadloc=s in highl concurrent sstems.

    e.g.true & false

    hibernate.generate_statistics

    If enabled5 Hibernate will collect statistics useful for performancetuning.

    e.g.true & false

    hibernate.use_identifier_rollbac=

    If enabled5 generated identifier properties will be reset to defaultvalues when ob0ects are deleted.

    e.g.true & false

    hibernate.use_s$l_comments

    If turned on5 Hibernate will generate comments inside the SQL5for easier debugging5 defaults to false.

    e.g.true & false

    Hibernate JDBC and Connection Properties

    Property name Purpose

    hibernate.0dbc.fetch_size  + non3zero value determines the !D"# fetch size 4callsStatement.set/etchSize466.

    hibernate.0dbc.batch_size

     + non3zero value enables use of !D"#> batch updates b

    Hibernate.

    e.g. recommended values between ? and 87

    hibernate.0dbc.batch_versioned_data

    Set this propert to true if our !D"# driver returns correct

    row counts from e1ecute"atch46. Iit is usuall safe to turn

    this option on. Hibernate will then use batched D*L for

    automaticall versioned data. Defaults to false.

    e.g.true & false

    hibernate.0dbc.factor_class

    Select a custom org.hibernate.0dbc."atcher. *ost

    applications will not need this configuration propert.

    e.g.classname.of."atcher/actor

    hibernate.0dbc.use_scrollable_resultset )nables use of !D"#> scrollable resultsets b Hibernate.

  • 8/16/2019 Question and answer in Hibernate

    8/12

    Property name Purpose

    This propert is onl necessar when using user3supplied

    !D"# connections. Hibernate uses connection metadata

    otherwise.

    e.g.true & false

    hibernate.0dbc.use_streams_for_binar

    @se streams when writing(reading binarserializable tpes

    to(from !D"#. *system-level property*  or

    e.g.true & false

    hibernate.0dbc.use_get_generated_=e

    s

    )nables use of !D"#8

    'reparedStatement.geteneratedAes46 to retrieve nativel

    generated =es after insert. Be$uires !D"#8C driver and

    !B);.9C5 set to false if our driver has problems with the

    Hibernate identifier generators. " default5 it tries to

    determine the driver capabilities using connection metadata.

    e.g.true&false

    hibernate.connection.provider_class

    The classname of a custom

    org.hibernate.connection.#onnection'rovider which

    provides !D"# connections to Hibernate.

    e.g.classname.of.#onnection'rovider 

    hibernate.connection.isolation

    Sets the !D"# transaction isolation level. #hec=

     0ava.s$l.#onnection for meaningful values5 but note thatmost databases do not support all isolation levels and some

    define additional5 non3standard isolations.

    e.g.;5 >5 95 :

    hibernate.connection.autocommit

    )nables autocommit for !D"# pooled connections 4it is not

    recommended6.

    e.g.true & false

    hibernate.connection.release_mode Specifies when Hibernate should release !D"#connections. " default5 a !D"# connection is held until the

    session is e1plicitl closed or disconnected. /or an

    application server !T+ datasource5 use after_statement to

    aggressivel release connections after ever !D"# call. /or 

    a non3!T+ connection5 it often ma=es sense to release the

    connection at the end of each transaction5 b using

    after_transaction. auto will choose after_statement for the

    !T+ and #*T transaction strategies and after_transaction

  • 8/16/2019 Question and answer in Hibernate

    9/12

    Property name Purpose

    for the !D"# transaction strateg.

    e.g.auto 4default6 & on_close & after_transaction &

    after_statement

    This setting onl affects Sessions returned from

    Session/actor.openSession. /or Sessions obtainedthrough Session/actor.get#urrentSession5 the

    #urrentSession#onte1t implementation configured for use

    controls the connection release mode for those Sessions.

    hibernate.connection. 'ass the !D"# propert Driver*anager.get#onnection46. to

    hibernate.0ndi. 'ass the propert to the !,DI Initial#onte1t/actor.

    Hibernate Cache Properties

    Property name Purpose

    hibernate.cache.provider_class

    The classname of a custom #ache'rovider.

    e.g.classname.of.#ache'rovider 

    hibernate.cache.use_minimal_puts

    -ptimizes second3level cache operation to minimize

    writes5 at the cost of more fre$uent reads. This setting is

    most useful for clustered caches and5 in Hibernate85 is

    enabled b default for clustered cache implementations.

    e.g.true&false

    hibernate.cache.use_$uer_cache

    )nables the $uer cache. Individual $ueries still have to

    be set cachable.

    e.g.true&false

    hibernate.cache.use_second_level_cache

    #an be used to completel disable the second level

    cache5 which is enabled b default for classes whichspecif a mapping.

    e.g.true&false

    hibernate.cache.$uer_cache_factor

    The classname of a custom

    Quer#acheStandardQuer#ache. interface5 defaults to

    the built3in

    e.g.classname.of.Quer#ache

  • 8/16/2019 Question and answer in Hibernate

    10/12

    Property name Purpose

    hibernate.cache.region_prefi1

     + prefi1 to use for second3level cache region names.

    e.g.prefi1

    hibernate.cache.use_structured_entries

    /orces Hibernate to store data in the second3level cache

    in a more human3friendl format.

    e.g.true&false

    Hibernate Transaction Properties

    Property name Purpose

    hibernate.transaction.factor_class

    The classname of a Transaction/actor to use with

    Hibernate Transaction +'I 4defaults to

    !D"#Transaction/actor6.

    e.g.classname.of.Transaction/actor

     0ta.@serTransaction

     + !,DI name used b !T+Transaction/actor to

    obtain the !T+ @serTransaction from the application

    server.

    e.g. 0ndi(composite(name

    hibernate.transaction.manager_loo=up_class

    The classname of a Transaction*anagerLoo=up. It is

    re$uired when !*3level caching is enabled or when

    using hilo generator in a !T+ environment.

    e.g.classname.of.Transaction*anagerLoo=up

    hibernate.transaction.flush_before_completio

    n

    If enabled5 the session will be automaticall flushed

    during the before completion phase of the transaction.

    "uilt3in and automatic session conte1t management is

    preferred.

    e.g.true & false

    hibernate.transaction.auto_close_session

    If enabled5 the session will be automaticall closed

    during the after completion phase of the transaction.

    "uilt3in and automatic session conte1t management is

    preferred.

    e.g.true & false

  • 8/16/2019 Question and answer in Hibernate

    11/12

    Miscellaneous Properties

    Property name Purpose

    hibernate.current_session_conte1t_clas

    s

    Suppl a custom strateg for the scoping of the 2current2

    Session. See Section >.?5 E#onte1tual sessionsF for more

    information about the built3in strategies.

    e.g. 0ta & thread & managed & custom.#lass

    hibernate.$uer.factor_class

    #hooses the HQL parser implementation.

    e.g.org.hibernate.h$l.ast.+STQuerTranslator/actor or

    org.hibernate.h$l.classic.#lassicQuerTranslator/actor

    hibernate.$uer.substitutions

    Is used to map from to=ens in Hibernate $ueries to SQL

    to=ens 4to=ens might be function or literal names5 for

    e1ample6.

    e.g.h$lLiteralGSQL_LIT)B+L5 h$l/unctionGSQL/@,#

    hibernate.hbm>ddl.auto

     +utomaticall validates or e1ports schema DDL to the

    database when the Session/actor is created. %ith create3

    drop5 the database schema will be dropped when the

    Session/actor is closed e1plicitl.

    e.g.validate & update & create & create3drop

    hibernate.cglib.use_reflection_optimizer 

    )nables the use of #LI" instead of runtime reflection

    4Sstem3level propert6. Beflection can sometimes beuseful when troubleshooting. Hibernate alwas re$uires

    #LI" even if ou turn off the optimizer. ou cannot set this

    propert in hibernate.cfg.1ml.

    e.g.true & false

    SQL Dialects

     +lwas set the hibernate.dialect propert to the correct org.hibernate.dialect.Dialect subclass for our

    database. If ou specif a dialect5 Hibernate will use sensible defaults for some of the other propertieslisted above. This means that ou will not have to specif them manuall.

    http://docs.jboss.org/hibernate/stable/core/reference/en/html/architecture.html#architecture-current-sessionhttp://docs.jboss.org/hibernate/stable/core/reference/en/html/architecture.html#architecture-current-session

  • 8/16/2019 Question and answer in Hibernate

    12/12

    Hibernate SQL Dialects hibernate.dialect!

    "DBMS Dialect

    D"> org.hibernate.dialect.D">Dialect

    D"> +S(977 org.hibernate.dialect.D">977Dialect

    D"> -S87 org.hibernate.dialect.D">87Dialect

    'ostgreSQL org.hibernate.dialect.'ostgreSQLDialect

    *SQL org.hibernate.dialect.*SQLDialect

    *SQL with InnoD" org.hibernate.dialect.*SQLInnoD"Dialect

    *SQL with *IS+* org.hibernate.dialect.*SQL*IS+*Dialect

    -racle 4an version6 org.hibernate.dialect.-racleDialect

    -racle i org.hibernate.dialect.-racleiDialect

    -racle ;7g org.hibernate.dialect.-racle;7gDialect

    Sbase org.hibernate.dialect.SbaseDialect

    Sbase +nwhere org.hibernate.dialect.Sbase+nwhereDialect

    *icrosoft SQL Server org.hibernate.dialect.SQLServerDialect

    S+' D" org.hibernate.dialect.S+'D"Dialect

    Informi1 org.hibernate.dialect.Informi1Dialect

    HpersonicSQL org.hibernate.dialect.HSQLDialect

    Ingres org.hibernate.dialect.IngresDialect

    'rogress org.hibernate.dialect.'rogressDialect

    *c=oi SQL org.hibernate.dialect.*c=oiDialect

    Interbase org.hibernate.dialect.InterbaseDialect

    'ointbase org.hibernate.dialect.'ointbaseDialect

    /ront"ase org.hibernate.dialect./rontbaseDialect

    /irebird org.hibernate.dialect./irebirdDialect