2014年2月7日星期五

How to write a lot of two-way relationship here ? Seeking to rationalize the idea

model : customers, orders , rooms , schedule



The first process:
guests submit an order to form an order object , order room contains objects object , the client object , schedule objects.

now proposed :

room objects

public class Room
{
private long id;

private String name; // 房型名称

private Float price; // 挂牌价
private Float realPrice; // 现售价格

private int breakfast; // '1':'含早餐','0':'无早餐'

private String info; // 简介


target customers


public class Customer
{
private long id;

private String realName;
private int gender; // '1':'男','0':'女'

private String identity; // 用户身份证号
private String nationality; // 用户国籍
private String address;

private String phone;
private String email;


schedule object


public class Schedule
{
private int id;

private Date orderDate;

private Date beginDate;

private Date endDate;







rationalize these comparisons , the corresponding hbm has also been established.



problem is now [ Order objects:


public class Book
{
private int id;

private Room room; // 客房

private Customer customer; // 客人信息

private Schedule schedule; // 日程


hbm not very clear that how to write , the idea is a bit of :
middle of the table is to use as many relationships in the Orders table .
but rooms can not be deleted ( the total amount of room on the hotel all that much ) , guest information can not be deleted ( guest history data ) , the schedule may be to clear ( schedule over several years of data can be deleted in the second schedule stages of development time and mobility might be a relationship , schedules and room objects in addition to a relationship , it will also provide tables and dining a relationship , a relationship and meetings arranged . )




These are now the first self- written program processes card to order here.





============================================== ============================================

An order may contain multiple clients , you need two people to live two guest information , guest information so I will change the order later collection.


out of the process to see outside.

a customer will contain multiple orders , an order will also contain a number of customers. ( Speaking here only for the room , do not take account of Meetings Orders )







============================================== ================

So I think the point here is his knowledge of the leak , do not write this hbm file , or modeling wrong.


two objects -many relationship , I would write , but how to write three objects -many relationship , accurately say how to form an intermediate table .
table structure like this :

middle table ID \ room table foreign key ID \ Guest Information foreign key ID \ schedule a foreign key ID








============================================== =========================
but I do not think I'm wrong , because I found the time to write and schedule an object when the object collaboration room when you can achieve control daily housekeeping work in this business ( that is a total of 20 such as Double , 1 enough 20 between the time can not be sold , the 2nd had 12 points after restored to 20 )

------ Solution ------------------------------------ such examples --------
many online ah , can be referenced .
help you find the two :
http://ryxxlong.iteye.com/blog/626416
http://www.blogjava.net/wujun/archive/2006/04/08/39956.html
------ Solution ------------- -------------------------------
suggest you look at the last hibernate document best practices , does not recommend the use of multi- for many , unless absolutely necessary !
------ Solution ---------------------------------------- ----
personally think that it is not necessary to configure this relationship . In the table there is any relationship to each other only save id on it, I need to use to get the associated record according to id .
In addition , 6th Floor, said contrary to hibernate learning purposes, this really does not. Really, the project is how quick and easy how to , but if you do it faster and better than using hibernate , then why do you have to use it too , because you are in the project using the hibernate it? To improve the efficiency of the tool . Like Xiao Ming and Zhang apart than the two 10 m yuan , each have a car , do not say it because a car , you must drive to go?
------ For reference only -------------------------------------- -
are not many relationships , should not go to room table , guest tables, schedules three table relationships


Instead, three relationships were established : Doula
Orders table with room table ( order date corresponds with a different room , an order can only be developed under a single chamber. )
Orders table -many table with guests
Orders table and the schedule of many-
------ For reference only -------------------------- -------------
Well, I now want to understand the point, seems to be the order of business logic , it is basically impossible to insert mounted directly SPRING Well , only in the business layer first realized the insertion schedule to get the schedule 's OID then insert orders.


But the order object is not to waste in order to remove what should be assembled when the object of it . Attempt. . . . . Later posted to another question or knot .


scholarship END SSH2 integrate two days , please do not place more than a professional guide ! ! !
------ For reference only -------------------------------------- -


Thank you , friends, and relive it again -many

look relatively late , hope do not mind, to go before the New Year . . . .
------ For reference only -------------------------------------- -



friend so, give a very simple example yesterday I tried to write it:

Users
team

A user can be in multiple groups, one group can have multiple users
============================================== =
database has users \ user_group \ groups of three tables

operations will be conducted as follows :
delete users watch users while removing user_group tables and user-related records, but to be able to keep groups table corresponds to the organization ;
delete groups table tissue while removing user_group tables and records related to the organization , but to be able to keep users table users ;
============================================== =
I think this is a typical many.

Now I'm confused a little something like this:
I can write two hbm, realized the middle of the table is automatically generated , but the operation can only be achieved when my own part of the operation , this is the place incomprehensible .

code is as follows :


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="bean.Group" table="groups">

<id name="id" column="id" type="long">
<generator class="increment"></generator>
</id>

<property name="groupName" column="group_name" type="string"></property>

<set name="users" table="user_group" cascade="save-update">
<key column="group_id"></key>
<many-to-many class="bean.User" column="user_id"></many-to-many>
</set>

</class>

</hibernate-mapping>





<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="bean.User" table="users">

<id name="id" column="id" type="long">
<generator class="increment"></generator>
</id>

<property name="username" column="username" type="string"></property>
<property name="password" column="password" type="string"></property>

<set name="groups" table="user_group" cascade="save-update">
<key column="user_id"></key>
<many-to-many class="bean.Group" column="group_id"></many-to-many>
</set>

</class>

</hibernate-mapping>






 //添加数据:正确实现添加内容
 User user1 = new User();
 user1.setUsername("zhangsan");
 user1.setPassword("123456");

 Group group1 = new Group();
 group1.setGroupName("组一");

 user1.getGroups().add(group1);
 group1.getUsers().add(user1);

 UserDAO userDAO = new UserDAOImpl();
 userDAO.save(user1);

//测试二:删除用户,可以实现删除中间表记录
// UserDAO userDAO = new UserDAOImpl();
// User user1 = userDAO.findUserById(1);
// userDAO.removeUser(user1);

//测试三:删除组,不能删除组,也不会删除中间表相关记录
// GroupDAO groupDAO = new GroupDAOImpl();
// Group group1 = groupDAO.findGroupById(1);
//
// System.out.println(group1);
//
// groupDAO.removeGroup(group1);



Now my difficulty is in the actual development is how to deal with such problems ?
because I can not hbm to manage this relationship , but only manage each corresponding model Tim excision investigation, and then go to their calling in the business layer dao to complete three different functional requirements .
something to do so I was very relaxed , but always felt that this is not contrary to hibernate for learning purposes ?


------ For reference only ---------------------------------- -----



Thank you, I have access to official documents , where you say , understand !
official recommendation : split into two many, most of the time need to connect the table to add additional information.

And this is what prompted my page perplexing question .
At this point, the front solved the same problem !


Do not use exotic association mappings:
Practical test cases for real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. ;. In fact, most associations are one-to-many and many-to-one For this reason, you should proceed cautiously when using any ;. other association style
------ For reference only ------------------------------- --------



Well , thank you, I understand, I previously thought hibernate in addition to help complete object into the database , but also reflected in the hbm possible mutual relationship, and by substituting treatment hibernate .

now, although not express it, but it seems more aware of the point .


============================================== =============
chose this because it finished SSH2 integrate learning , if disarray side processing , write well hbm file , the next step would not be able hbm file as a resource into the spring configuration file.

past six months I have to look for a solution.

more than a year before he wrote a lot of code that can be said for generations .
front from the beginning of writing HTML + CSS + JS start
Later in the maintenance of soft , U8 database SQL SERVER2000
background and then later write ASP code and the kind of language mixing ( to launch late simply pull a body, they did not write my own method modified ) After
PHP and then later saw the video section , starting with a simple MVC pattern to write , but too fragmented (perhaps as a mature language has its own solution , but I did not stick to it ) .
Later that because of the mobile client , write your own copy or imitate a android client to the unit used ( in which the video made ​​strong Java the language ) , due to the presence android client , their scope of work expand again , multilingual maintenance as a person who is largely my burden , but also understand what they do best is to deal with that piece of content.

( front than I'm good , because they belong to a perfectionist , always in front of their own are unable to meet their own doing now want to think, so give up ; database side , then put it crudely , Stuck cases , also so many things ; so that business last block of )

So six months looking at Java tutorial, I locate their position further , try to use other aspects of the mature thing to do instead of their own , after reading the SSH2 integration can be said to be excited , because finally find their focus a .

没有评论:

发表评论