我的目标是学习JavaEE,以及比在项目上工作更好的学习方法。因此,我着手创建一个股票市场模拟Web应用程序。
自然,一个人拥有一些由公司代码(company_id)标识的股票以及相关数量的股票。所以我把它们放在地图上。

这是mysql ddl语句;
对于用户表

    CREATE TABLE `users` (
     `user_id` bigint(20) NOT NULL AUTO_INCREMENT,
     `firstName` varchar(30) NOT NULL,
     `lastName` varchar(30) NOT NULL,
      PRIMARY KEY (`user_id`)
     ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;

For the portfolios table,

     CREATE TABLE `portfolios_tb` (
       `user_id` bigint(20) NOT NULL,
       `company_id` varchar(4) NOT NULL,
       `shares_owned` bigint(20) NOT NULL DEFAULT '0',
        PRIMARY KEY (`user_id`,`company_id`),
        KEY `company_id` (`company_id`),
        CONSTRAINT `company_id` FOREIGN KEY (`company_id`) REFERENCES `stocks` (`company_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
        CONSTRAINT `uid` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


The Stock entity

    import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

/**
 *

 */
@Entity
@Table(name = "stocks")
public class Stock implements Serializable {

    @Id
    @GeneratedValue
    @Column(name = "company_id")
    String stockId;

    @NotNull @Column(name="company_name")
    String companyName;

    @NotNull @Column(name="shares_listed")
    BigInteger sharesListed;

    @Column(name="par_value")
    BigDecimal parValue;

    @Column(name="current_Price")
    BigDecimal currentPrice;

    public Stock(){

    }

    public Stock(String stockId, String companyName, BigInteger sharesListed){
        this.companyName = companyName;
        this.stockId = stockId;
        this.sharesListed = sharesListed;
        this.parValue = BigDecimal.ZERO;
        this.currentPrice = BigDecimal.ZERO;
    }

    public String getStockId() {
        return stockId;
    }

    public void setStockId(String stockId) {
        this.stockId = stockId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public BigInteger getSharesListed() {
        return sharesListed;
    }

    public void setSharesListed(BigInteger sharesListed) {
        this.sharesListed = sharesListed;
    }

    public BigDecimal getParValue() {
        return parValue;
    }

    public void setParValue(BigDecimal parValue) {
        this.parValue = parValue;
    }

    public BigDecimal getCurrentPrice() {
        return currentPrice;
    }

    public void setCurrentPrice(BigDecimal currentPrice) {
        this.currentPrice = currentPrice;
    }


}


The User entity

    import java.io.Serializable;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;


    @Entity
    @Table(name = "users")
    public class User implements Serializable {

        @Id
        @GeneratedValue
        private String user_id; //I know its not convention, was experimenting.

        @Column(name = "firstName")
        private String firstName;

        @Column(name = "lastName")
        private String lastName;

        @ElementCollection
        @CollectionTable(name = "portfolios_tb")
        @MapKeyColumn(name = "company_id")
        @Column(name = "shares_owned")
        Map<String, BigInteger> stocksOwned = new HashMap<>();


        public User() {


        }

        public User(String firstName, String lastName) {
            this.stocksOwned = new HashMap<>();
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getUser_id() {
            return user_id;
        }

        public void setUser_id(String user_id) {
            this.user_id = user_id;
        }

        public String getFirstName() {
            return firstName;
        }

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

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public Map<String, BigInteger> getStocksOwned() {
            return stocksOwned;
        }

        public void setStocksOwned(Map<String, BigInteger> stocksOwned) {
            this.stocksOwned = stocksOwned;
        }

    }


这是主班

public class Main {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("hisaMarket_Version2PU");
    EntityManager em = emf.createEntityManager();

    public static void main(String[] args) {
        Main main = new Main();

        main.getUsers();
    }
public void getUsers(){
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        TypedQuery<User> query = em.createQuery("SELECT u FROM User u", User.class);
        List<User> users = query.getResultList();

        for(User user : users){
            System.out.print(user.getFirstName() + " "+ user.getLastName() +" owns ");
            Map<String,BigInteger> stocks = user.getStocksOwned();

            Set<String> keys = stocks.keySet();
            //planning to display Map key and corresponding value

            System.out.println();
        }
        tx.commit();
        em.close();
        emf.close();
    }


当我运行它时,我从eclipselink收到此消息

Error Code: 1054
Call: SELECT t0.shares_owned, t0.company_id FROM portfolios_tb t0 WHERE (t0.User_USER_ID = ?)
    bind => [1 parameter bound]
Query: DataReadQuery(name="stocksOwned" sql="SELECT t0.shares_owned, t0.company_id FROM portfolios_tb t0 WHERE (t0.User_USER_ID = ?)")


为什么eclipselink将实体名(User)和enitityId(user_id)串联起来以赋予此t0.User_USER_ID =?而不是这个“ .... to.user_id”

最佳答案

那是因为您没有为该映射指定@JoinColumn,所以JPA的默认机制是生成联接列名称,例如<entity_name>_<id_column_name>

只需在@JoinColumn(name = "user_id")地图映射上添加@CollectionTable属性,它就可以工作。

@ElementCollection
@CollectionTable(name = "portfolios_tb", joinColumns = @JoinColumn(name = "user_id"))
@MapKeyColumn(name = "company_id")
@Column(name = "shares_owned")
Map<String, BigInteger> stocksOwned = new HashMap<>();

关于mysql - 使用eclipselink将映射映射到表会导致eclipselink将sql查询中的实体名称和EntityId串联起来,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29213470/

10-16 06:26