A public schoolboy who claimed he was sleepwalking when he tried to murder two students and a teacher in a hammer attack has been detained for life.