many-to-one lazy

3620 ワード

//Books.hbm.xmlのラベル属性(lazy)を削除すると、次の文は表示されません.原因不明

package com.hibernate.entity;

import java.io.Serializable;

public class Publishers implements Serializable {

	public int id;
	
	public String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

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

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.hibernate.entity.Publishers" table="publishers" catalog="bookshop">
		<id name="id" type="java.lang.Integer">
			<column name="Id" />
			<generator  class="native"></generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="Name" length="16" not-null="true" />
		</property>
		
	</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.hibernate.entity.Books" table="Books" catalog="bookshop">
		<id name="id" type="java.lang.Integer">
			<column name="Id" />
			<generator  class="native"></generator>
		</id>
		<property name="title" type="java.lang.String">
			<column name="title" length="16" not-null="true"></column>
		</property>
		<many-to-one name="Publishers" column="PublisherId" class="com.hibernate.entity.Publishers"></many-to-one>
	</class>
</hibernate-mapping>

package com.hibernate.entity;

import java.io.Serializable;

public class Books implements Serializable {
	
	private int id;
	
	private Publishers publishers;

	public Books(){}

	public Publishers getPublishers() {
		return publishers;
	}

	public void setPublishers(Publishers publishers) {
		this.publishers = publishers;
	}

	public int getId() {
		return id;
	}

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

package com;

import org.junit.Test;

import com.hibernate.dao.BaseHibernateDAO;
import com.hibernate.entity.Books;

public class Select extends BaseHibernateDAO {

	@Test
	public void select() {
		Books books = (Books) super.get(Books.class, 4939);
		//  (Books.hbm.xml) (many-to-one) (lazy) , 。  
		System.out.println("name=" + books.getPublishers().getName());
	}
	
}