在JPABootApplication over Hibernate中我需要定义一个带有2个PK的表 我将另一个类定义为PK并用@Embeddable注释它 在实体类中,我有PK的成员用@EmbeddableId进行了分配。 这是代码:
帐户:
package demo;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="ACCOUNTS")
public class Account {
@EmbeddedId
private AccountId accountId;
private String upassword;
private String fName;
private String lName;
private boolean isManager;
public Account() {
// TODO Auto-generated constructor stub
}
public Account(AccountId accountId, String upassword, String fName, String lName, boolean isManager) {
super();
this.accountId = accountId;
this.upassword = upassword;
this.fName = fName;
this.lName = lName;
this.isManager = isManager;
}
public AccountId getAccountId() {
return accountId;
}
public void setAccountId(AccountId accountId) {
this.accountId = accountId;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public boolean isManager() {
return isManager;
}
public void setManager(boolean isManager) {
this.isManager = isManager;
}
}
ACCOUNTID:
package demo;
import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.GeneratedValue;
@Embeddable
public class AccountId implements Serializable {
private Long accId;
private String uName;
public AccountId() {
// TODO Auto-generated constructor stub
}
@GeneratedValue
public Long getAccId() {
return accId;
}
public void setAccId(Long accId) {
this.accId = accId;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((accId == null) ? 0 : accId.hashCode());
result = prime * result + ((uName == null) ? 0 : uName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AccountId other = (AccountId) obj;
if (accId == null) {
if (other.accId != null)
return false;
} else if (!accId.equals(other.accId))
return false;
if (uName == null) {
if (other.uName != null)
return false;
} else if (!uName.equals(other.uName))
return false;
return true;
}
}
现在,我正在尝试向帐户添加帐户: 包演示;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"demo" })
public class TestApplication implements CommandLineRunner{
@Autowired
private AccountManager accountManager;
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
AccountId aid1 = new AccountId();
aid1.setuName("AAAA");
Account a1 = new Account(aid1, "0212", "KKKK", "SSS", false);
Long a1id = accountManager.addNewAccount(a1);
System.err.println(a1id + ") " + a1);
}
}
然后我因插入Null而得到Exception。我希望将accId设为AutoGenerated(有@GeneratedValue注释):
2017-06-12 20:44:18.440 WARN 7200 --- [main] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:515,SQLState:23000 2017-06-12 20:44:18.441 ERROR 7200 --- [main] ohengine.jdbc.spi.SqlExceptionHelper:无法将值NULL插入列" acc_id',table' trading。 dbo.accounts11&#39 ;;列不允许空值。 INSERT失败。 2017-06-12 20:44:18.442 INFO 7200 --- [main] o.h.e.j.b.internal.AbstractBatchImpl:HHH000010:在批量发布时它仍然包含JDBC语句 2017-06-12 20:44:18.447 INFO 7200 --- [主要] utoConfigurationReportLoggingInitializer:
我怎样才能让它发挥作用?