5

I am using: graphql-spring-boot and graphql-java-tools for the implementation.

movie.graphqls

type Movie {
    id: Short
    name: String
    poster: String
    releaseDate: String
    runtime: String
    storyline: String
    rated: String
    rating: String
    inserted: String
}


type Query {
    movies: [Movie]
    movie(id: ID!): Movie
}

Movie model

@Entity
public class Movie {
    private Short id;
    private String name;
    private String poster;
    private Date releaseDate;
    private Time runtime;
    private String storyline;
    private String rated;
    private double rating;
    private Timestamp inserted;
}

As you can see i have no relationship with other models.
finally the class which implement GraphQLQueryResolver

@Component
public class Query implements GraphQLQueryResolver {
    @Autowired
    private MovieRepository movieRepository;
    public List<Movie> movies() {
        return this.movieRepository.findAll();
    }
    public Movie movie(Short id) {
        return this.movieRepository.getOne(id);
    }
}

the following query works fine:

{
  movies{
    name
    rating
  }
}

but this query:

{
  movie(id: 1){
    name
  }
}

gives me the following error:

Exception while fetching data (/movie/rated) : could not initialize proxy [com.example.demo.model.Movie#1] - no Session

1
  • not the same question. the problem in that question is the relationship between models, here i have no relationship ! Commented Aug 31, 2018 at 12:26

2 Answers 2

5
+50

Try changing getOne to findOne

public Movie movie(Short id) {
    return this.movieRepository.findOne(id);
}

I think using getOne requires the call to be within a transaction context. Hence the error - no session.

Sign up to request clarification or add additional context in comments.

1 Comment

Had the same issue, I've spent 2 hours trying to solve not the actual problem that I had and you saved me, thanks! Would never guess that getById is different than findById.
0

you need to add the naming of strategy in your conguration file application.properties

hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

1 Comment

Cannot resolve configuration property 'hibernate.ejb.naming_strategy'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.