Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Hibernate Basics Persisting Data with Hibernate Updating and Deleting Entities

Cannot perform the (int) cast as shown in the video

While following along with the video for Hibernate Basics -> Persisting Data with Hibernate -> Updating and Deleting Entities, I have encountered a problem while attempting to enter the line:

int id = (int) session.save(contact)

which is shown starting at @4:30 in the video.

Chris types this in and everything looks fine, when I typed the exact same line in I received an error message in IntelliJ which reads: 'Incovertible types; cannot cast 'java.io.Serializable' to 'int'.

Any help would be much appreciated.

2 Answers

Alexander Nikiforov
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

The problem is the following.


I think you have to change this line in your build.gradle

sourceCompatibility = 1.5

To this line

sourceCompatibility = 1.8

After that you should refresh Gradle and it will Work


The reason why Chris does not have error and you do is in the way you have created project in intellijdea.

If you read this Stack post

You will see that Intellijdea by default creates separate module per source set.


It also sets sourceCompatibility = 1.5.

This line sourceCompatibility = 1.5 will play Role only when you set

Create Separate Sources per module set  

If you do then, IDE will think that you use JAVA 1.5

and Because you use JDK 1.5. Serializable cannot be converted to int

In JDK 1.8 however this can be done.

Why For Chris was Ok and For you not

Because Chris made that video long ago and back then

Create Separate Sources per module set

Was set to false by default. I.e. sourceCompatibility = 1.5 was not read and JDK was 1.8

That is why he did not have problems


If my guess was correct you should also see red squiggly lines under all stream and forEach expresssions.

Alexander, thank you very much for your response. That simple change worked just as you stated.

Have a virtual high-five!