`
lijunabc
  • 浏览: 47494 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hibernate array 数组映射

阅读更多
person.java
package com.aabnn.vo;

public class Person {
	private int id;
	private String name;
	private String[] schools;
	public Person(){}
	public Person(String name,String[] schools){
		this.name=name;
		this.schools=schools;
	}
	public int getId() {
		return id;
	}
	@SuppressWarnings(value="unused")
	private void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String[] getSchools() {
		return schools;
	}
	public void setSchools(String[] schools) {
		this.schools = schools;
	}
	
}

Person.hbm.xml
<?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 package="com.aabnn.vo">
  <class name="Person" table="person">
  	<id name="id" column="id" type="integer">
  		<generator class="identity" />
  	</id>
  	<property name="name" column="name" type="string" />
  	<array name="schools" table="schools">
  		<key column="id"/>
  		<list-index column="listindex" />
  		<element type="string" column="name" />
  	</array>
  </class>
</hibernate-mapping>

Test.java
package com.aabnn.test;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.aabnn.vo.Person;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SessionFactory sf=new Configuration().configure().buildSessionFactory();
		Session sess = sf.openSession();
		Transaction tx=sess.beginTransaction();
		String[] schools={"xiaoxue","zhongxue","daxue"};
		Person p=new Person("xiaoxiao",schools);
		sess.save(p);
		tx.commit();
		sess.close();
	}

}

mysql
-- Create schema collectionmapping
--

CREATE DATABASE IF NOT EXISTS collectionmapping;
USE collectionmapping;

--
-- Definition of table `person`
--

DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(45) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `person`
--

/*!40000 ALTER TABLE `person` DISABLE KEYS */;
INSERT INTO `person` (`id`,`name`) VALUES 
 (4,'xiaoxiao'),
 (5,'xiaoxiao');
/*!40000 ALTER TABLE `person` ENABLE KEYS */;


--
-- Definition of table `schools`
--

DROP TABLE IF EXISTS `schools`;
CREATE TABLE `schools` (
  `id` int(10) unsigned NOT NULL,
  `name` varchar(45) NOT NULL default '',
  `listindex` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `schools`
--

/*!40000 ALTER TABLE `schools` DISABLE KEYS */;
INSERT INTO `schools` (`id`,`name`,`listindex`) VALUES 
 (4,'daxue',2),
 (4,'xiaoxue',0),
 (4,'zhongxue',1),
 (5,'daxue',2),
 (5,'xiaoxue',0),
 (5,'zhongxue',1);
/*!40000 ALTER TABLE `schools` ENABLE KEYS */;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics