-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Embedded Object can't set some null members?
PostPosted: Wed Jun 06, 2012 3:35 am 
Newbie

Joined: Wed May 30, 2012 8:49 pm
Posts: 2
Hello.

I'm using hibernate 4.1.3.Final.
I try to search by embeddable object.
If it exists null column inner embeddable object, I get NoResultException everytime.
Can't Embedded Object set some null members?

Embeddable Object Class.
Code:
@Embeddable
public class EmbeddableNames {
   
    /** First Name */
    private String firstName;
   
    /** Middle Name(nullable) */
    private String middleName;
   
    /** Family Name */
    private String familyName;

    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }
    public String getFamilyName() { return familyName; }
    public void setFamilyName(String familyName) { this.familyName = familyName; }
    public String getMiddleName() { return middleName; }
    public void setMiddleName(String middleName) { this.middleName = middleName; }
}


Entity Class.
Code:
@Entity
@Table(name= "Sample")
public class Sample implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
   
    @Embedded
    private EmbeddableNames embeddableNames;

    public EmbeddableNames getEmbeddableNames() { return embeddableNames; }
    public void setEmbeddableNames(EmbeddableNames embeddableNames) { this.embeddableNames = embeddableNames; }
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

}


TestCode datainput
Code:
        EmbeddableNames eastName = new EmbeddableNames();
        eastName.setFirstName("Tarou");
        eastName.setFamilyName("YAMADA");
        // east name doesnot hav middlename.

        Sample sample = new Sample();
        sample.setEmbeddableNames(eastName);
        entityManager.persist(sample);


TestCode dataSearch
Code:
        EmbeddableNames names = new EmbeddableNames();
        names.setFirstName("Tarou");
        names.setFamilyName("YAMADA");
        // middlename is null.

        Sample result = entityManager.createQuery("from Sample where embeddableNames = :names ", Sample.class).setParameter("names", names).getSingleResult();


Top
 Profile  
 
 Post subject: @embeddable nullable attribute problem
PostPosted: Fri Jun 15, 2012 2:44 am 
Newbie

Joined: Wed May 30, 2012 8:49 pm
Posts: 2
Hello.
I tried to my code in hibernate 4.1.4 final , unfortunately it does not work.
Hibernate's nullable attribute at @embedded doesn't work?

My code show below:
Code:
/**
* Sample Entity
*
* @author nobukick
*/
@Entity
@Table(name= "Sample")
public class Sample implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
   
    @Embedded
    private EmbeddableNames embeddableNames;

    private String comment;

    public Sample() {}

    public EmbeddableNames getEmbeddableNames() {
        return embeddableNames;
    }

    public void setEmbeddableNames(EmbeddableNames embeddableNames) {
        this.embeddableNames = embeddableNames;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Long getId() {
        return id;
    }

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



Sample.class is JPA's entity. This embedded EmbeddableNames.class.

Code:
/**
* Sample Attributes
*
* @author nobukick
*/
@Embeddable
public class EmbeddableNames implements Serializable {
   
    /** First Name */
    @Column(name = "FirstName")
    private String firstName;
   
    /** Middle Name(nullable) */
    @Column(name = "MiddleName", nullable=true)
    private String middleName;
   
    /** Family Name */
    @Column(name = "FamilyName")
    private String familyName;
   
    public EmbeddableNames() {}

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFamilyName() {
        return familyName;
    }

    public void setFamilyName(String familyName) {
        this.familyName = familyName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }
}


I figured middle name column is nullable, because many Asian peoples doesn't have that.

Code:
/**
* Embedded Object Search Test
*
* @author nobukick
*/
public class EmbeddableObjectSearchTest extends TestCase {

    private EntityManagerFactory entityManagerFactory;

    @Override
    protected void setUp() throws Exception {
        entityManagerFactory = Persistence.createEntityManagerFactory("testPU");
        createData();
    }

    @Override
    protected void tearDown() throws Exception {
        entityManagerFactory.close();
    }

    /**
     * create data;
     */
    private void createData() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();

        /**
         * west sample
         */
        EmbeddableNames westName = new EmbeddableNames();
        westName.setFirstName("Dorothy");
        // set middle name
        westName.setMiddleName("Walker");
        westName.setFamilyName("Bush");

        Sample westComment = new Sample();
        westComment.setEmbeddableNames(westName);
        westComment.setComment("I am Dorothy Walker Bush.");

        entityManager.persist(westComment);

        /**
         * east sample
         */
        EmbeddableNames eastName = new EmbeddableNames();
        eastName.setFirstName("Tarou");
        eastName.setFamilyName("YAMADA");
        // east name doesnot hav middlename.

        Sample eastComment = new Sample();
        eastComment.setEmbeddableNames(eastName);
        eastComment.setComment("I am tarou yamada.");

        entityManager.persist(eastComment);

        entityManager.getTransaction().commit();
        entityManager.close();
    }

    /**
     * Test west name origin
     */
    public void testWestNameOriginSearch() {

        EntityManager entityManager;

        // now lets pull events from the database and list them
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
       
        // this case is success
        EmbeddableNames names = new EmbeddableNames();
        names.setFirstName("Dorothy");
        names.setMiddleName("Walker");
        names.setFamilyName("Bush");
       
        Sample result = entityManager.createQuery("from Sample where embeddableNames = :names ", Sample.class).setParameter("names", names).getSingleResult();
       
        entityManager.getTransaction().commit();
        entityManager.close();

    }

    /**
     * Test east name origin
     * east name does not hav middlename(eq null);
     */
    public void testEastNameOriginSearch() {

        EntityManager entityManager;

        // now lets pull events from the database and list them
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();

        EmbeddableNames names = new EmbeddableNames();
        names.setFirstName("Tarou");
        names.setFamilyName("YAMADA");


        Sample result = entityManager.createQuery("from Sample where embeddableNames = :names ", Sample.class).setParameter("names", names).getSingleResult();
       
        assertEquals(result.getComment(), "I am tarou yamada.");

        entityManager.getTransaction().commit();
        entityManager.close();
    }
}


Quote:
javax.persistence.NoResultException: No entity found for query
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:290)
at net.noumin.hibernatesample.entity.EmbeddableObjectSearchTest.testEastNameOriginSearch(EmbeddableObjectSearchTest.java:109)
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:601)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:115)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:102)
at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
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:601)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)


I expected EntityManager returned Tarou's entity after persisting it.

But EntityManager returned NoResultException.

How to do EntityManager returned it?
Has anyone seen this problem before?
I guess this is @embedded bugs.

Thanks


Top
 Profile  
 
 Post subject: Re: Embedded Object can't set some null members?
PostPosted: Fri Jun 15, 2012 3:25 pm 
Newbie

Joined: Fri Jun 15, 2012 3:22 pm
Posts: 1
Hi,

I am facing same issue here. If anybody got solved it please let me know...

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.