我使用spring数据jpa,这是我的样本:
public interface UserRepository extends JpaRepository<User, Long> {
User findByUserName(String userName);
....}
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTests {
@Autowired
private UserRepository userRepository;
@Test
public void test1(){
String name = userRepository.getOne(3L).getUserName();
}
}
@Entity
public class User extends Entitys implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false, unique = true)
private String userName;
..}
Run Code Online (Sandbox Code Playgroud)
test1将抛出"LazyInitializationException:无法初始化代理 - 无会话",但如果我使用userRepository.findByUserName("aa").getUserName()就可以了.虽然问题可以通过添加@Transactional来解决,但我想知道其背后的原因和原因.
我在/sf/answers/2406965361/中找到了anwser的一部分,但是findByUserName中的事务如何工作?
我使用 mvn clean package docker:build 来调用机器 B 中的 dockerfile(docker version 18.03.1-ce ):
FROM openjdk:8-jdk-alpine
RUN apk update && apk upgrade && apk add netcat-openbsd && apk add curl
Run Code Online (Sandbox Code Playgroud)
事实证明:
Step 2/8 : RUN apk update && apk upgrade && apk add netcat-openbsd && apk add curl
---> Running in 89c9b97b9d75
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.7/main: temporary error (try again later)
WARNING: Ignoring APKINDEX.70c88391.tar.gz: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我发现这是网络问题,机器B通过机器A访问互联网,我尝试在 /etc/docker/daemon.json 中添加“dns”,在 ~/.docker/config.json 中添加“httpProxy”,现在我成功运行:
`docker run -it cc2179b8f042`
apk update …Run Code Online (Sandbox Code Playgroud) 我阅读了https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/。我尝试了像这样的建议配置(使用spring数据JPA,hibernate 5.0作为供应商):
public class PaperSubjectType{
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private PaperSetting paperSetting;
..
}
class PaperSetting{
@Id
@GeneratedValue
private Long id;
..
}
Run Code Online (Sandbox Code Playgroud)
首先,我尝试了以下示例:
PaperSetting paperSettingInDb = paperSettingRepository.findOne(1);
PaperSubjectType paperSubjectType = new PaperSubjectType();
paperSubjectType.setSubjectCode("91");
paperSubjectType.setPaperSetting(paperSettingInDb);
paperSubjectTypeRepository.save(paperSubjectType);
Run Code Online (Sandbox Code Playgroud)
错误:已将分离的实体传递给persist:PaperSetting。似乎在级联时休眠将PaperSetting视为已分离
2如果我想同时创建PaperSubjectType和PaperSetting,我需要这样做:
PaperSetting paperSetting = new PaperSetting();
paperSetting.setxx;
PaperSetting paperSettingInDbNew = paperSettingRepository.save(paperSetting);
PaperSubjectType paperSubjectType = new PaperSubjectType();
paperSubjectType.setPaperSetting(paperSettingInDbNew);
paperSubjectTypeRepository.save(paperSubjectType);
Run Code Online (Sandbox Code Playgroud)
还是在这种情况下应该使用双向?谢谢?
关于竞争条件的"开始行动"的样本:
var (
counter int
wg sync.WaitGroup
)
func main() {
wg.Add(2)
go incCounter(1)
go incCounter(2)
wg.Wait()
fmt.Println("Final Counter:", counter)
}
func incCounter(id int) {
defer wg.Done()
for count := 0; count < 2; count++ {
value := counter
//1 fmt.Println("value=",value)
runtime.Gosched()
value++
counter = value
//2 fmt.Println("counter=",counter)
}
}
Run Code Online (Sandbox Code Playgroud)
最后计数器最终应该是2,这里解释说:"每个goroutine都会覆盖另一个goroutine的工作.这发生在goroutine交换发生的时候.每个goroutine都会自己制作一个计数器变量的副本,然后进行交换当goroutine被给予时间再次执行时,计数器变量的值已经改变,但是goroutine不会更新它的副本.而是继续增加它的副本并将值设置回计数器变量,替换其他goroutine执行的工作."
我猜这是环境原因,我的机器输出4与1.10.3 + win10.我想知道自这本书发布以来发生了什么变化?如果我取消注释标记1最终计数器打印2或随机,如果我取消注释标记2.为什么?