Seam实体不会持久化数据库

时间:2011-12-30 08:45:32

标签: database hibernate entity seam persist

我使用seam框架来开发一个网站。我想在开发时间时分享一个问题。我有一个接缝测试方法成功;

    @Test
    public void saveTwoCategoryToRoot() throws Exception {
    new FacesRequest() {

        Category enduringRoot = new Category(
                ItemTreeVocabulary.ROOTNODE_NAME);

        protected void beforeRequest() {
            // clear database..
            clearDb();

            enduringRoot.addChild(new Category(
                    ItemTreeVocabulary.COMPUTER));

            enduringRoot.addChild(new Category(
                    ItemTreeVocabulary.WHITE_GOODS));
        };

        protected void invokeApplication() throws Exception {

            // set the root to database..
            ((EntityManager) getValue("#{entityManager}")).persist(enduringRoot);

            // feth the root from database..
            @SuppressWarnings("rawtypes")
            List resultList = ((EntityManager) getValue("#{entityManager}")).createQuery(
                    "Select i From Item i").getResultList();

            Category root = (Category) resultList.get(0);

            // list shuld not to be empty..
            Assert.assertFalse(resultList.isEmpty());

            // checks the nodes in the first depth..
            **Assert.assertEquals(2, root.getChildren().size());**                   

            // assert rootNode name..
            Assert.assertEquals(((Category) resultList.get(0)).getName(),
                    ItemTreeVocabulary.ROOTNODE_NAME);
            // assert first node in first depth..
            Assert.assertEquals(((Category) resultList.get(0))
                    .getChildren().get(0).getName(),
                    ItemTreeVocabulary.COMPUTER);
            // assert second node in first depth..
            Assert.assertEquals(root
                    .getChildren().get(0).getName(),
                    ItemTreeVocabulary.WHITE_GOODS);
        };

    }.run();

}

类别类是;

@Entity
public class Category extends Item implements Serializable {

private static final long serialVersionUID = -1154500438874768209L;

private List<Item> children;

public Category() {
}

public Category(String name) {
    this();
    setName(name);
}

@OneToMany(targetEntity=Item.class, mappedBy = "parent",cascade = CascadeType.ALL)
public List<Item> getChildren() {
    return children;
}

public void addChild(Item child) {
    if (children == null) {
        children = new ArrayList<Item>();
    }

    if (!children.contains(child)) {
        child.setParent(this);
        children.add(child);
    }
}

public void setChildren(List<Item> children) {
    this.children = children;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Category other = (Category) obj;
    if (children == null) {
        if (other.children != null)
            return false;
    } else if (!children.equals(other.children))
        return false;
    return true;
}

}

项目类别是;

@Inheritance(strategy = InheritanceType.JOINED)
@Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = "name") })
public class Item implements Serializable {

private static final long serialVersionUID = 8332532835833777840L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return id;
}

/**
 * Hold the item's parent.
 */
private Category parent;

/**
 * Hold the item's name.
 */
private String name;

private Long id;

@Transient
public List<Item> getChildren() {
    return null;
}

@ManyToOne(targetEntity=Category.class)
public Category getParent() {
    return parent;
}

public void setParent(Category parent) {
    this.parent = parent;
}


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

public String getName() {
    return name;
}

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

按照这种方法,当我运行测试时,下面出现关于粗线的异常;

 FAILED: saveOneCategoryToRoot
java.lang.AssertionError: expected:<2> but was:<1>
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.failNotEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:126)
at org.junit.Assert.assertEquals(Assert.java:470)
at org.junit.Assert.assertEquals(Assert.java:454)
at com.galaksiya.kobar.test.itemtree.CategoryTest$2.invokeApplication(CategoryTest.java:105)
at org.jboss.seam.mock.AbstractSeamTest$Request.invokeApplicationPhase(AbstractSeamTest.java:656)
at org.jboss.seam.mock.AbstractSeamTest$Request.emulateJsfLifecycle(AbstractSeamTest.java:605)
at org.jboss.seam.mock.AbstractSeamTest$Request.access$100(AbstractSeamTest.java:177)
at org.jboss.seam.mock.AbstractSeamTest$Request$2.doFilter(AbstractSeamTest.java:505)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
at org.jboss.seam.mock.AbstractSeamTest$Request.run(AbstractSeamTest.java:499)
at org.jboss.seam.mock.AbstractSeamTest$FacesRequest.run(AbstractSeamTest.java:872)
at com.galaksiya.kobar.test.itemtree.CategoryTest.saveOneCategoryToRoot(CategoryTest.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:676)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1169)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1182)
at org.testng.TestRunner.privateRun(TestRunner.java:761)
at org.testng.TestRunner.run(TestRunner.java:612)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
at org.testng.SuiteRunner.run(SuiteRunner.java:241)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1169)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1094)
at org.testng.TestNG.run(TestNG.java:1006)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:107)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:199)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:170)

我不明白为什么'whiteGoods'的实例不是保存数据库。

提前致谢..

1 个答案:

答案 0 :(得分:2)

问题是由于当您将某个项目添加到某个类别的子项时,只有在该项目尚未包含在子项目中时才会执行此操作。而类别的equels方法如果它们具有相同的子节点则认为两个类别相等,即使它们没有相同的名称。所以你们两个都是平等的。