-->
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.  [ 2 posts ] 
Author Message
 Post subject: Ask a solution about entities
PostPosted: Tue Nov 17, 2015 12:08 am 
Newbie

Joined: Mon Nov 16, 2015 11:11 pm
Posts: 2
Quote:
table A: (PK: id)
ID NAME OTHER
1 a1 ...
2 a2 ...
3 a3 ...

p.s.:
`NAME` is unique. It's logical primary key on table A. And `ID` is true pk.

Quote:
table B: (PK: id)
ID A_NAME GROUP OTHER
1 a1 g1 ...
2 a1 g2 ...
3 a2 g2 ...
4 a4 g3 ...

p.s.:
`A_NAME` and `GROUP` is together unique. These are logical composite primary key. `ID` is true pk.
Table B have not foreign key about A. But logically, table B.A_NAME = A.NAME(like B.A_ID = A.ID, but we don't design A_ID for table B).

I want to query 'SELECT * FROM A, B WHERE B.A_NAME = A.NAME'. what should I design this two table's Entities? (1)

I do it like the blow codes. but it throw exception:
Code:
@Entity
@Table(name="A")
public class A {
     @ID
     private Integer id;
     @OneToMany(mappedBy="b")
     private String name;
}


Code:
@Entity
@Table(name="B")
public class B {
    @ID
    private Integer id;
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="A_NAME", referencedColumnName="NAME", nullable=false),
    private String a_name;
}


Exceptions:
Caused by: org.hibernate.MappingException: Unable to find column with logical name: GroupNo in org.hibernate.mapping.Table(DmGoodsStore) and its related supertables and secondary tables
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:583)
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:258)
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1598)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1521)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1422)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
... 29 more


Top
 Profile  
 
 Post subject: Re: Ask a solution about entities
PostPosted: Tue Nov 17, 2015 6:22 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
I haven't tried it, but it should be something like this:

Code:
@Entity
@Table(name="A")
public class A {
     @Id
     private Integer id;

     @NaturalId
     @Column(name="NAME", nullable=false)
     private String name
}

@Entity
@Table(name="B")
public class B {
    @Id
    private Integer id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="A_NAME", referencedColumnName="NAME", nullable=false),
    private A a_name;
}


Davide


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.