0

in JPABootApplication over Hibernate I need to define a table with 2 PK I definded another class to be the PK and annotated it with @Embeddable and in the entity class I have member of that PK anotated with @EmbeddableId. here is the code:

Account:

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;
    }


}

Now, I'm trying to add account to the DB: package demo;

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);


    }
}

But then I get Exception for inserting Null. I want to have the accId to be AutoGenerated (there is the @GeneratedValue annotation):

2017-06-12 20:44:18.440 WARN 7200 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 515, SQLState: 23000 2017-06-12 20:44:18.441 ERROR 7200 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Cannot insert the value NULL into column 'acc_id', table 'trading.dbo.accounts11'; column does not allow nulls. INSERT fails. 2017-06-12 20:44:18.442 INFO 7200 --- [ main] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements 2017-06-12 20:44:18.447 INFO 7200 --- [ main] utoConfigurationReportLoggingInitializer :

How can I make it work?

K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
NaorDavid2
  • 51
  • 2
  • 6
  • I don't think you can use generated in an Embeddable class. Maybe https://stackoverflow.com/questions/4446991/jpa-embeddedid-is-not-generating-sequence or https://stackoverflow.com/questions/24088776/null-id-generated-via-generatedvalue-on-composite-key-using-embeddedid helps? – K.Nicholas Jun 12 '17 at 18:43
  • thanks @KarlNicholas for now I left only one field as ID and added check if there is another row with the same uname... I think I'll try the "isNew" solution.. – NaorDavid2 Jun 13 '17 at 05:43

0 Answers0