Hibernate -- CirclesOfImage Application
Purpose:
The requirement is to find all the circles in an image and save them to the database.
Therefore, the database is designed for an image,
Save its image name (the image is stored in a unified directory, so you only need to save the image name) to save all the circle information in it, because the number of circles in each image is unknown, therefore, using List to save all circles as custom types, including Circle center (x, y) and radius custom type Circle
As shown in, I have Circle in Package c.
The circle contains the variable x, y record center, radius record radius, and a String variable strCircle record circle information. Why is it used.
Complete their get and set methods!
Storage Type CirclesOfImage
The Storage types are as follows:
intID of the type
StringImageUrl type
List Circles
This class is stored in the hibernate package.
Hibernate. cfg. xml
Create this file in src. The specific database ing file set here is Circles. hdb. xml in the hibernate package.
Circles. hdb. xml
I created it in the hibernate package. The primary key is id, there is an imageUrl column, and a column that stores the List. The List is another table, which is referenced by a foreign key here.
Test. java Test class
package hibernate; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import c.Circle; public class Test { /** * @param args */ private SessionFactory sessionFactory; private Configuration configuration; private ServiceRegistry serviceRegistry; private Transaction transaction; private Session session; public Test(){ sessionFactory = null; configuration = new Configuration().configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); session = sessionFactory.openSession(); transaction = session.beginTransaction(); } public void destroy(){ transaction.commit(); session.close(); sessionFactory.close(); } public void test(){ CirclesOfImage circlesOfImage = new CirclesOfImage(); circlesOfImage.setImageUrl(abc.jpg); List
circles = new ArrayList
(); for (int i = 0; i < 10; i++){ Circle c = new Circle(i, i+1, i+2); circles.add(c.getStrCircle()); } circlesOfImage.setCircles(circles); session.save(circlesOfImage); } public void get(){ CirclesOfImage circlesOfImage = (CirclesOfImage)session.get(CirclesOfImage.class, 1); System.out.println(circlesOfImage.toString()); } public static void main(String[] args) { // TODO Auto-generated method stub Test t = new Test(); t.test(); t.get(); t.destroy(); } }
There are three questions: Why?
List Instead
ArrayList To record circles?
2. Why do we use the String type to record the Circle instead of directly using the Circle type? In mysql
List How can I store the data types? First question
If I useArrayList Then, it is written
ArrayList circles = new ArrayList ();
This error will be reported:
Expected type: java. util. List, actual value: org. hibernate. collection. internal. PersistentSetup
I can see the solution at [http://blog.csdn.net/hollboy/article/details/9006043. Http://blog.csdn.net/hollboy/article/details/9006043)
** Here is the analysis: Hibernate has rewritten all the collections in jdk at the top level to facilitate the collection lazy loading operation, so it only needs to be saved and then retrieved, hibernate has converted the Data Type
Therefore, when the domain is defined, the attribute cannot be defined as a specific class, but an interface (for example, Set rather than HashSet). Otherwise, a type exception will be reported in hibernate. **
Second question
If I write
ArrayList circles = new ArrayList ();
This error will be reported:
Org. hibernate. MappingException: cocould not determine type for: c. Circle
This is how I wrote the ing file.
It seems that the ing is correct. My class is in c. in Circle, however, type is written to hibernate. hibernate supports some types, that is, the types that can be identified and used for storage, other custom categories cannot be identified. Maybe there is a way, but here I will simply use String to store Circle!
You can view the available ing types in hibernate.
Http://www.cnblogs.com/aijava/archive/2008/06/04/2191859.html
Third question
I see
[Http://blog.sina.com.cn/s/blog_86f4242c0101fmkp.html. Http://blog.sina.com.cn/s/blog_86f4502c0101fmkp.html)
You can simply create a new table and store it in the above way. It is amazing that you can change the storage length.
Lab results
There are indeed two more tables in the database.
Let's take a look at their specific storage.