使用Hibernate Lazy加载时出现意外行为

时间:2014-03-14 08:28:36

标签: java hibernate lazy-loading

我在查询父表(BeanMetaData)后尝试查看子表(BeanPropertyMetaData)的属性。我无法在控制台上看到子表值的输出。我也没有得到任何异常或错误信息。你能帮我辨认出错的地方吗?我在春季3使用Hiberate 3.源代码如下:

父类:

@Entity 
@Table(name="BEAN_META_DATA")
public class BeanMetaData extends GeneralEntity {

private String beanName;
private String mappedTable;
private String title;
private List<BeanPropertyMetaData> properties;

@Id
@Column(name = "D_PK", nullable = false, precision = 22, scale = 0)
@TableGenerator( name = "appSeqStore", table = "APP_SEQ_STORE", pkColumnName = "APP_SEQ_NAME", pkColumnValue = "BEAN_PK", valueColumnName = "APP_SEQ_VALUE", initialValue = 1, allocationSize = 1 )
@GeneratedValue( strategy = GenerationType.TABLE, generator = "appSeqStore" )
public long getPk() {
    return pk;
}

@Column(name = "BEAN_NAME", nullable = false, length=60)
public String getBeanName() {
    return beanName;
}
public void setBeanName(String beanName) {
    this.beanName = beanName;
}

@Column(name = "TABLENAME", nullable = false, length=60)
public String getMappedTable() {
    return mappedTable;
}
public void setMappedTable(String mappedTable) {
    this.mappedTable = mappedTable;
}

@Column(name = "TITLE", nullable = false, length=60)
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}


**@OneToMany(mappedBy="beanName",cascade = { javax.persistence.CascadeType.ALL}, fetch = FetchType.LAZY)
public List<BeanPropertyMetaData> getProperties() {
    return properties;
}**

public void setProperties(List<BeanPropertyMetaData> props) {
    this.properties = props;
} 

}

儿童班:

@Entity
@Table(name="BEAN_PROPERTY_DATA")
public class BeanPropertyMetaData extends GeneralEntity {

String propertyName;
String mappedColumn;
DataType dataType;
String label;
boolean isUnique;
boolean isMandatory;
boolean showInList;
int listSeq;
int listColLength;
BeanMetaData beanName;
BeanPropertyMetaData refBean;


@Id
@Column(name = "D_PK", nullable = false, precision = 22, scale = 0)
@TableGenerator( name = "appSeqStore", table = "APP_SEQ_STORE", pkColumnName = "APP_SEQ_NAME", pkColumnValue = "BEAN_PROPERTY_PK", valueColumnName = "APP_SEQ_VALUE", initialValue = 1, allocationSize = 1 )
@GeneratedValue( strategy = GenerationType.TABLE, generator = "appSeqStore" )
public long getPk() {
    return pk;
}

@Column(name = "PROPERTY_NAME", nullable = false, length=60)
public String getPropertyName() {
    return propertyName;
}
public void setPropertyName(String propertyName) {
    this.propertyName = propertyName;
}

@Column(name = "COLUMN_NAME", length=60)
public String getMappedColumn() {
    return mappedColumn;
}

public void setMappedColumn(String mappedColumn) {
    this.mappedColumn = mappedColumn;
}

@Column(name = "DATATYPE", length=60)
@Enumerated(EnumType.STRING)
public DataType getDataType() {
    return dataType;
}
public void setDataType(DataType dataType) {
    this.dataType = dataType;
}

@Column(name = "LABEL", length=60)
public String getLabel() {
    return label;
}
public void setLabel(String label) {
    this.label = label;
}

@Column(name = "IS_UNIQUE")
public boolean isUnique() {
    return isUnique;
}
public void setUnique(boolean isUnique) {
    this.isUnique = isUnique;
}

@Column(name = "IS_NULL")
public boolean isMandatory() {
    return isMandatory;
}
public void setMandatory(boolean isMandatory) {
    this.isMandatory = isMandatory;
}

@Column(name = "IN_LIST")
public boolean isShowInList() {
    return showInList;
}
public void setShowInList(boolean showInList) {
    this.showInList = showInList;
}

@Column(name = "LIST_SEQ",precision = 22, scale = 0 )
public int getListSeq() {
    return listSeq;
}
public void setListSeq(int listSeq) {
    this.listSeq = listSeq;
}

@Column(name = "LIST_LENGTH", precision = 22, scale = 0)
public int getListColLength() {
    return listColLength;
}
public void setListColLength(int listColLength) {
    this.listColLength = listColLength;
}

**@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "FK_BEAN_META_DATA", nullable = false)
public BeanMetaData getBeanName() {
    return beanName;
}**
public void setBeanName(BeanMetaData beanName) {
    this.beanName = beanName;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "FK_BEAN_PROPERTY")
public BeanPropertyMetaData getRefBean() {
    return refBean;
}
public void setRefBean(BeanPropertyMetaData refBean) {
    this.refBean = refBean;
}

}

我在数据库中的两个表中都填充了值。我正在进行以下测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-db-config.xml", "classpath:spring-beans.xml"})
public class BeanServiceTest {

@Autowired 
DatabaseDAO databaseDAO;

@Test
public void getBeanMetaData(){
    String beanName= "com.inspireme.ebeans.Project";
    String query= "from BeanMetaData as metadata where beanName= '"+beanName+"'";
    System.out.println("1");
    SessionFactory sf= databaseDAO.getSessionFactory();
    System.out.println("2");
    //Session session = sf.getCurrentSession();
    Session session = sf.openSession();
    System.out.println("3");
    session.beginTransaction();
    System.out.println("4");
    Query hibQuery= session.createQuery(query);
    System.out.println("5");
    List result= hibQuery.list();
    System.out.println("6");
    BeanMetaData beanData= (BeanMetaData) result.get(0);
    System.out.println("7");
    System.out.println("bean data= "+beanData.getTitle()+", pk= "+beanData.getPk());
    System.out.println("9");
    List<BeanPropertyMetaData> properties= beanData.getProperties();
    System.out.println("10");
    System.out.println("prop size= "+properties.size());
    System.out.println("11");
    for(BeanPropertyMetaData prop: properties){
        System.out.println("prop name= "+prop.getLabel());

    }
    session.close();
    assertTrue(true);
}

我在控制台上获得以下输出:

0 [main] INFO com.inspireme.utils.PropertiesUtil - 从类路径资源[database.properties]加载属性文件
1
2
3
4
5
休眠:选择beanmetada0_.D_PK ...
6
7
bean data = Project,pk = 2
9
10
休眠:选择properties0_.FK_BEAN_META_DATA asFK15_2_,properties0_.D_PK为D1_2_,properties0_.D_PK为D1_6_1_,properties0_.D_CREATED_BY为D2_6_1_,properties0_.D_CREATED为D3_6_1_,properties0_.D_MODIFIED_BY为D4_6_1_,properties0_.D_MODIFIED为D5_6_1_,properties0_.FK_BEAN_META_DATA为FK15_6_1_, properties0_.DATATYPE为DATATYPE6_1_,properties0_.LABEL为LABEL6_1_,properties0_.LIST_LENGTH为LIST8_6_1_,properties0_.LIST_SEQ为LIST9_6_1_,properties0_.IS_NULL为IS10_6_1_,properties0_.COLUMN_NAME为COLUMN11_6_1_,properties0_.PROPERTY_NAME为PROPERTY12_6_1_,properties0_.FK_BEAN_PROPERTY为FK16_6_1_,properties0_。 IN_LIST为IN13_6_1_,properties0_.IS_UNIQUE为IS14_6_1_,beanproper1_.D_PK为D1_6_0_,beanproper1_.D_CREATED_BY为D2_6_0_,beanproper1_.D_CREATED为D3_6_0_,beanproper1_.D_MODIFIED_BY为D4_6_0_,beanproper1_.D_MODIFIED为D5_6_0_,beanproper1_.FK_BEAN_META_DATA为FK15_6_0_,beanproper1_.DATATYPE作为DATATYPE6_0_,beanproper1_.LABEL为LABEL6_0_,bean proper1_.LIST_LENGTH如LIST8_6_0_,beanproper1_.LIST_SEQ如LIST9_6_0_,beanproper1_.IS_NULL如IS10_6_0_,beanproper1_.COLUMN_NAME如COLUMN11_6_0_,beanproper1_.PROPERTY_NAME如PROPERTY12_6_0_,beanproper1_.FK_BEAN_PROPERTY如FK16_6_0_,beanproper1_.IN_LIST如IN13_6_0_,beanproper1_.IS_UNIQUE如IS14_6_0_从BEAN_PROPERTY_DATA properties0_左外连接BEAN_PROPERTY_DATA beanproper1_ on properties0_.FK_BEAN_PROPERTY = beanproper1_.D_PK其中properties0_.FK_BEAN_META_DATA =?

为什么程序不打印列表大小?任何帮助是极大的赞赏。

0 个答案:

没有答案