maven 프로젝트 hibenate 한 쌍 의 다 중 주석 예

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.orm</groupId>
	<artifactId>db</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>db</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>4.3.6.Final</version>
		</dependency>

		<dependency>
			<groupId>postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>9.1-901-1.jdbc4</version>
		</dependency>

		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

	</dependencies>

</project>

hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1/db_cloud</property>
		<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
		<property name="hibernate.connection.username">postgres</property>
		<property name="hibernate.connection.password">admin</property>
		<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		<property name="hibernate.current_session_context_class">thread</property>  
      	<mapping class="cn.orm.vo.User"/>
      	<mapping class="cn.orm.vo.Topic"/>
	</session-factory>
</hibernate-configuration>
package cn.orm.vo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="t_user2")
public class User 
{
	@Id
	@GeneratedValue
	@Column(name = "uid")
	private Integer id;
	
	@Column
	private String username;
	
	@Column
	private String nickname;
	
	@Column
	private String stype;
	
	@OneToMany(cascade=CascadeType.ALL)  
    @JoinColumn(name="uid") //            
	private List<Topic> topics = new ArrayList<Topic>();

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getStype() {
		return stype;
	}

	public void setStype(String stype) {
		this.stype = stype;
	}

	public List<Topic> getTopics() {
		return topics;
	}

	public void setTopics(List<Topic> topics) {
		this.topics = topics;
	}
	
	public String toString()
	{
		return "[id="+id+",username="+username+",nickname="+nickname+"]";
	}
}
package cn.orm.vo;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_topic2")
public class Topic
{
	@Id
	@GeneratedValue
	@Column(name = "tid")
	private Integer id;
	
	@ManyToOne(cascade = CascadeType.ALL, optional = false)
	@JoinColumn(name="uid")//   uid, User  id  
	private User user;
	
	@Column
	private String title;
	
	@Column
	private String content;
	
	@Column(name = "t_status")
	private String status;
	
	@Column(name = "t_type")
	private String type;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public String toString()
	{
		return "[user.uid="+user.getId()+",title="+title+"]";
	}
}
package cn.orm.db;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import cn.orm.vo.Topic;
import cn.orm.vo.User;

/**
 * Hello world!
 *
 */
public class App 
{	
	static SessionFactory sf;
	
	static {
		sf =  new AnnotationConfiguration().configure().buildSessionFactory();
	}
	
	public static void init() throws Exception 
	{

		Session session = sf.openSession();
		
		Transaction ts=session.beginTransaction();
		
		//ts.begin();

//		User u1 = new User();
//		u1.setUsername("tomcat");
//		u1.setNickname("web   ");
//		u1.setStype("0");
		
		User u1 = (User)session.load(User.class, 22);
		
		Topic t = new Topic();
		t.setContent("xx");
		t.setTitle("Apache Tomcat v7.0.1    ");
		t.setStatus("0");
		t.setType("0");
		t.setUser(u1);

		//session.save(u1);
		session.save(t);

		
		ts.commit();

		session.close();

		sf.close();

	}

	public static void query() throws Exception 
	{

		Session session = sf.openSession();
		
		User u1 = (User)session.load(User.class, 22);
		
		List<Topic> list = u1.getTopics();
		
		for(Topic topic : list)
		{
			System.out.println(topic + "\t" + topic.getUser());
		}
		
		sf.close();
		
	}
	
	public static void main(String[] args) throws Exception {
		query();
	}
}

좋은 웹페이지 즐겨찾기