betterjava commented on a change in pull request #122: Add a broadcast-table example for sharding-jdbc URL: https://github.com/apache/incubator-shardingsphere-example/pull/122#discussion_r272996198
########## File path: example-common/repository-jpa/src/main/java/org/apache/shardingsphere/example/common/jpa/repository/CountryRepositoryImpl.java ########## @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.example.common.jpa.repository; + +import org.apache.shardingsphere.example.common.entity.Country; +import org.apache.shardingsphere.example.common.repository.CountryRepository; +import org.springframework.stereotype.Repository; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import javax.transaction.Transactional; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +@Repository +@Transactional +public class CountryRepositoryImpl implements CountryRepository { + + private static final AtomicLong ID_INCREASE = new AtomicLong(); + + @PersistenceContext + private EntityManager entityManager; + + @Override + public void createTableIfNotExists() { + throw new UnsupportedOperationException("createTableIfNotExists for JPA"); + } + + @Override + public void dropTable() { + throw new UnsupportedOperationException("truncateTable for JPA"); + } + + @Override + public void truncateTable() { + throw new UnsupportedOperationException("dropTable for JPA"); + } + + @Override + public Long insert(final Country country) { + long id = ID_INCREASE.incrementAndGet(); + country.setId(id); Review comment: see this code in ‘org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingStatement’ ``` @Override public ResultSet getGeneratedKeys() throws SQLException { Optional<GeneratedKey> generatedKey = getGeneratedKey(); if (returnGeneratedKeys && generatedKey.isPresent()) { return new GeneratedKeysResultSet(routeResult.getGeneratedKey().getGeneratedKeys().iterator(), generatedKey.get().getColumnName(), this); } if (1 == getRoutedStatements().size()) { return getRoutedStatements().iterator().next().getGeneratedKeys(); } return new GeneratedKeysResultSet(); } ``` 'entityManager.persist(country)' will invoke this method ,but no GeneratedKeys return. Then it throws Exception. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
