1 Import Java. util. date;
2
3 Import Javax. Persistence. Basic;
4 Import Javax. Persistence. column;
5 Import Javax. Persistence. entity;
6 Import Javax. Persistence. enumtype;
7 Import Javax. Persistence. enumerated;
8 Import Javax. Persistence. fetchtype;
9 Import Javax. Persistence. generatedvalue;
10 Import Javax. Persistence. ID;
11 Import Javax. Persistence. lob;
12 Import Javax. Persistence. Table;
13 Import Javax. Persistence. Temporal;
14 Import Javax. Persistence. temporaltype;
15 Import Javax. Persistence. Transient;
16
17 @ Entity
18 @ Table (name = "persontable ")
19 Public Class Person {
20 @ ID
21 @ Generatedvalue
22 Private Integer ID;
23 @ Column (length = 10, nullable = False , Name = "personname ")
24 Private String name;
25 @ Temporal (temporaltype. Date)
26 Private Date birthday;
27 @ Enumerated (enumtype. String)
28 @ Column (length = 5, nullable = False )
29 Private Gender gender = gender. Man;
30 @ Lob // The database field corresponding to the declarative attribute is a large text class, and the file attribute is also mapped using this declarative attribute.
31 Private String Info;
32 @ Lob // The declared attribute corresponds to a big file data field.
33 @ Basic (fetch = fetchtype. Lazy) // Set to delayed loading. This field is not retrieved when we retrieve this record in the database.
34 Private Byte [] file;
35 @ Transient // This annotation is used to indicate that the ImagePath attribute is not used as a persistent field, that is, it is not associated with any database field.
36 Private String ImagePath;
37 Omitting the get set method ......
38 }
The generated database field types are as follows:
assume that private byte [] file is saved as a file. If we want to obtain a person object, we will retrieve the content saved in the file field, and put it in the memory. (If the file we save is 50 MB, the file will be obtained every time we get the person bean. The file may be 50.1 MB in memory, which occupies too much resources, what should I do ?) You can add @ basic (fetch = fetchtype. lazy) This annotation. If we set delayed loading, when we call the get method of Hibernate to obtain the person record, if the get method does not access the file attribute, it will not get the file from the database. If you want to access this file attribute, then it loads the file data from the database. That is to say, as long as we do not access it, it will not load data from the database into the memory. If the file is not loaded, the person record may be around M. Of course, if you access the file attribute, it will load the data again from the database, the memory may be MB. Therefore, the @ basic label is generally used for big data. That is to say, if the data size you store is large, the @ basci label should be used if the data size exceeds 1 MB, if the attribute is delayed for initialization, data will not be loaded immediately when the person object is obtained for the first time, but file data will be loaded only when the first access is made. Of course, when you first access the file, make sure that the entitymanager object is open (just like the session object is open) . If the entitymanager object is closed, when we access its latency attribute, there will be exceptions to delayed loading, which is also frequently encountered in hibernate.