Country by country code
justnulling Nov 6, 2008 11:22 AMNPE when using String for primary key. As with country code (US) being the key.
Here is sample code.
@Entity @Table(uniqueConstraints = @UniqueConstraint(columnNames = "CountryName")) public class Country implements java.io.Serializable { private String countryId; // "US" private String countryName; // "United States" public Country() { } @Id @Column(name = "CountryID", unique = true, nullable = false, length = 2) public String getCountryId() { return countryId; } @Column(name = "CountryName", unique = true, nullable = false, length = 50) public String getCountryName() { return countryName; } public void setCountryId(String countryId) { this.countryId = countryId; } public void setCountryName(String countryName) { this.countryName = countryName; } }
@Entity @Table(uniqueConstraints = @UniqueConstraint(columnNames = "UserName")) public class User implements java.io.Serializable { private Integer userId; private int version; private Country country; private String userName; @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "UserID", unique = true, nullable = false) public Integer getUserId() { return userId; } @JoinColumn(name = "Country", nullable = false) public Country getCountry() { return country; } @Column(name = "UserName", unique = true, nullable = false) public String getUserName() { return userName; } @Version @Column(name = "Version", nullable = false) public int getVersion() { return version; } public void setUserId(Integer userId) { this.userId = userId; } public void setCountry(Country country) { this.country = country; } public void setUserName(String userName) { this.userName = userName; } public void setVersion(int version) { this.version = version; } }
public class UserHome extends EntityHome<User> { @Override public User find() { final User user = super.find(); Country country = user.getCountry(); // <-- NPE } }
user.getCountry() line throws a NPE
Also have tried with FetchType.EAGER
@ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "Country", nullable = false) public Country getCountry() { return country; }
and it also throws NPE.
The sql it produces looks good and returns both CountryID and CountryName fine.
select
user0_.UserID as UserId1_6_1_,
user0_.Country as Country6_1_,
user0_.UserName as UserName6_1_,
user0_.Version as Version6_1_,
country1_.CountryID as CountryID18_0_,
country1_.CountryName as CountryN3_18_0_,
from
Tbl.User user0_
inner join
Tbl.Country country1_
on user0_.Country=country1_.CountryID
where
user0_.UserID=?
Any ideas?
Thank you