Hibernate annotation problem (exception) Set

Source: Internet
Author: User

1. Problem: An annotationconfiguration instance is required to use <mapping class = "VO. Student"/>

Answer: Error: sessionfactory factory = new configuration (). Configure (). buildsessionfactory (); // The ing file is XML.
Correct: sessionfactory factory = new annotationconfiguration (). Configure (). buildsessionfactory (); // This sentence is used for Annotation

2. Problem: @ onetoone or @ manytoone on VO. stucour. Student references an unknown entity: VO. Student

Answer: Error cause (1): No ing is added to the configuration file.
Method: add the following configuration: In hibernta. HFG. xml <mapping class = "VO. Student"/>
Cause of error (2): No entity annotation is added to the object class public class classname,
Method: Add annotation: @ entity
@ Table (name = "student", catalog = "SMS ")

3. Problem: mappedby reference an unknown target entity property: VO. stucour. stucours
Cause of error: in the one-to-pair annotation configuration: @ onetoworkflow (mappedby = "stucour"), mappedby points to the attribute to be associated, not the class to be associated,
If this is configured, Hibernate will find the stucours attribute under the vo. stucour class. However, if this attribute is not used, the above exception will be reported.
Method: specify the actual associated attribute: @ onetoworkflow (mappedby = "courseid ")

4. Problem: org. hibernate. lazyinitializationexception: cocould not initialize proxy-No session

Cause of error: In manytoone, onetoone, and manytomany, the configuration is to be loaded by default, (Fetch = fetchtype.Lazy)

Method: Cancel:Fetch = fetchtype.Lazy, Or change:Fetch = fetchtype.Eager

Other related: When querying data in hibernate, We will write as follows:

 
Session session =Configuration (). Configure (). buildsessionfactory (). opensession (); Student Stu=(Student) Session. Get (ID );Session. Close ();ReturnStu;

 

In fact, when we return an object, we have closed the session, but we are using it elsewhere: course c = Stu. getcourse (); because it isFetch = fetchtype.LazySo the query is performed only when the call is made, but from the aboveCodeIt can be seen that the session has been closed, so the above exception will be reported.

There are many solutions to this problem:

1), to increase the transaction to the service layer.

 

2) use Spring AOP for management.

3) opensessionview can also be used for web projects.

 

5. Problem: cocould not resolve property: username of: COM. XM. model. User

Cause: hibernate uses hql. hql only recognizes fields in the class and does not recognize fields in the database table. This is because the user table in the database has the following attributes: username, in the class user. the attribute in Java is: @ column (name = "username") Private string username; when spelling hql, code snippets like this: where username =? , So this error occurs;

Method: Where username =? Note the fields in hql when spelling hql. The table name corresponds to the class name and attribute name of the Java class, rather than the table name and field name of the database.

 

6. Problem: annotation multi-to-one (manytoone) Problem

@ Entity (name = "lender" ) Public class lender implements java. Io. serializable {@ ID @ generatedvalue (Strategy = Identity) @ column (name = "ID", unique = True , Nullable = False ) Integer ID; @ Enumerated (enumtype. String) @ column (name = "Lenderdocumentationcommunication" ) Private lenderdocumentationcommunication; @ manytoone (optional = False , Cascade = Cascadetype. All) @ joincolumn (name = "Lenderheadofficeid" ) Private Office lenderheadoffice; @ manytoone (optional = False , Cascade = Cascadetype. All) @ joincolumn (name = "Lenderunderwritingofficeid" ) Private Office lenderunderwritingoffice; //  Omit other methods }

The many-to-one above is multiple objects of a class with different names.

Solution: As shown in the preceding figure,@ Manytoone (Cascade =Cascadetype. All)

Here: @ joincolumn name: Specifies the field name of the foreign key for production. The default value is: Class name_id,

From the above we can see that this name is used to specify the foreign key, so if multiple classes of an object, the name cannot be the same.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.