[color=#FF0000]hibernate 映射文件配置:
版面映射文件[/color]
<class name="Board" table="tb_board" >
<id name="boardId" column="board_id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="boardTitle" column="board_title" not-null="false" length="100" type="java.lang.String"/>
<property name="boardDescribe" column="board_describe" not-null="false" length="2000" type="java.lang.String"/>
<property name="boardThreadcount" column="board_threadcount" not-null="false" type="java.lang.Integer"/>
<property name="boardReplycount" column="board_replycount" not-null="false" type="java.lang.Integer"/>
<!-- 一对多关系 -->
<many-to-one name="boardCategory" column="category_id" class="com.leonse.bbs.category.bean.Category" lazy="false" fetch="join"/>
<!-- 多对多面板对用户 -->
<set name="boardPerson" table="tb_person_board" lazy="true" inverse="true" cascade="save-update">
<key column="board_id" />
<many-to-many class="com.leonse.bbs.person.bean.Person" column="person_id" />
</set>
</class>用户映射文件
<property name="personUsername" column="person_username" not-null="false" length="30" type="java.lang.String"/>
<property name="personPassword" column="person_password" not-null="false" length="16" type="java.lang.String"/>
<property name="personName" column="person_name" not-null="false" length="30" type="java.lang.String"/>
<property name="personSex" column="person_sex" not-null="false" length="2" type="java.lang.String"/>
<property name="personEmail" column="person_email" not-null="false" length="80" type="java.lang.String"/>
<property name="personRegistertime" column="person_registertime" not-null="false" length="30" type="java.lang.String"/>
<property name="personIp" column="person_ip" not-null="false" length="30" type="java.lang.String"/>
<property name="personTimelast" column="person_timelast" not-null="false" length="30" type="java.lang.String"/>
<property name="personIplast" column="person_iplast" not-null="false" length="30" type="java.lang.String"/>
<!-- 多对多 用户对面板 -->
<set name="personBoard" table="tb_person_board" lazy="false" inverse="false" cascade="save-update">
<key column="person_id" />
<many-to-many class="com.leonse.bbs.board.bean.Board" column="board_id" />
</set>单元测试类:public class IntroTest {
private static PersonService personService;
private static BoardService boardService; @BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
personService = (PersonService)act.getBean("personServiceBean");
boardService = (BoardService)act.getBean("boardServiceBean");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Test
public void save(){
Person person=new Person();
Board board=new Board();
Set<Board> personBoard=new HashSet<Board>() ;
person=(Person)personService.getObject(Person.class, 6);
board = (Board)boardService.getObject(Board.class, 13);
personBoard.add(board);
person.setPersonBoard(personBoard);
personService.updateObject(person);
}
}中间表数数据
person_id board_id
6 16
7 7
7 8
8 9
8 10
9 9
9 10
15 13
18 166是现在和16 关联 但是我想把 board_id 13 和person_id 6也关联起来 运行单元测试后 6关联16的这条记录被删除, 只剩下 6关联13控制台消息多了一条 删除语句Hibernate: delete from tb_person_board where person_id=?
Hibernate: insert into tb_person_board (person_id, board_id) values (?, ?)
请问大神们,怎么让后来添加的关联数据的时候不删除原来的关联数据。 先过谢过了。hibernate多对多
版面映射文件[/color]
<class name="Board" table="tb_board" >
<id name="boardId" column="board_id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="boardTitle" column="board_title" not-null="false" length="100" type="java.lang.String"/>
<property name="boardDescribe" column="board_describe" not-null="false" length="2000" type="java.lang.String"/>
<property name="boardThreadcount" column="board_threadcount" not-null="false" type="java.lang.Integer"/>
<property name="boardReplycount" column="board_replycount" not-null="false" type="java.lang.Integer"/>
<!-- 一对多关系 -->
<many-to-one name="boardCategory" column="category_id" class="com.leonse.bbs.category.bean.Category" lazy="false" fetch="join"/>
<!-- 多对多面板对用户 -->
<set name="boardPerson" table="tb_person_board" lazy="true" inverse="true" cascade="save-update">
<key column="board_id" />
<many-to-many class="com.leonse.bbs.person.bean.Person" column="person_id" />
</set>
</class>用户映射文件
<property name="personUsername" column="person_username" not-null="false" length="30" type="java.lang.String"/>
<property name="personPassword" column="person_password" not-null="false" length="16" type="java.lang.String"/>
<property name="personName" column="person_name" not-null="false" length="30" type="java.lang.String"/>
<property name="personSex" column="person_sex" not-null="false" length="2" type="java.lang.String"/>
<property name="personEmail" column="person_email" not-null="false" length="80" type="java.lang.String"/>
<property name="personRegistertime" column="person_registertime" not-null="false" length="30" type="java.lang.String"/>
<property name="personIp" column="person_ip" not-null="false" length="30" type="java.lang.String"/>
<property name="personTimelast" column="person_timelast" not-null="false" length="30" type="java.lang.String"/>
<property name="personIplast" column="person_iplast" not-null="false" length="30" type="java.lang.String"/>
<!-- 多对多 用户对面板 -->
<set name="personBoard" table="tb_person_board" lazy="false" inverse="false" cascade="save-update">
<key column="person_id" />
<many-to-many class="com.leonse.bbs.board.bean.Board" column="board_id" />
</set>单元测试类:public class IntroTest {
private static PersonService personService;
private static BoardService boardService; @BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
personService = (PersonService)act.getBean("personServiceBean");
boardService = (BoardService)act.getBean("boardServiceBean");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Test
public void save(){
Person person=new Person();
Board board=new Board();
Set<Board> personBoard=new HashSet<Board>() ;
person=(Person)personService.getObject(Person.class, 6);
board = (Board)boardService.getObject(Board.class, 13);
personBoard.add(board);
person.setPersonBoard(personBoard);
personService.updateObject(person);
}
}中间表数数据
person_id board_id
6 16
7 7
7 8
8 9
8 10
9 9
9 10
15 13
18 166是现在和16 关联 但是我想把 board_id 13 和person_id 6也关联起来 运行单元测试后 6关联16的这条记录被删除, 只剩下 6关联13控制台消息多了一条 删除语句Hibernate: delete from tb_person_board where person_id=?
Hibernate: insert into tb_person_board (person_id, board_id) values (?, ?)
请问大神们,怎么让后来添加的关联数据的时候不删除原来的关联数据。 先过谢过了。hibernate多对多
你只是想在原来的基础上新增关联数据应该这样
personBoard不应该在new了直接用持久态的personBoard
person=(Person)personService.getObject(Person.class, 6);
board = (Board)boardService.getObject(Board.class, 13);
Set<Board> personBoard=person.getPersonBoard();//用原来的personBoard这样就不会删掉原来的数据了
personBoard.add(board);
person.setPersonBoard(personBoard);
personService.updateObject(person);