A California teen was sentenced to 21 years in jail on Monday for shooting to death a 15-year-old gay middle school classmate who he said made unwanted sexual advances.

The sentencing of Brandon McInerney, the result of a plea deal with Ventura County prosecutors, brings to a close an emotionally charged case that previously resulted in a hung jury on a charge of murder and hate crime.

McInerney, who was 14 at the time of the shooting in 2008, was sentenced to 21 years in California state prison with not reduction for good behavior or credit for the four years he already served in prison, prosecutors said.

Prosecutors said McInerney, now 17, took a gun to his middle school in Oxnard and sat in a computer lab with classmate Larry King before shooting him in the back of the head and then firing at him as he lay on the ground.

King died two days later after he was removed from life support at a local hospital.

I'm relieved for the family of Larry King that this case is finally over, Ventura County prosecutor Maeve Fox told Reuters. I am sad to some degree that the jury could not convict him of murder in the first place, but it will allow both McInerney and the King family to put this behind them and move forward.

Prosecutors initially charged McInerney as an adult and accused him of murder with hate crime and gun use enhancements, in a case that was moved to suburban Los Angeles over concerns he might not get a fair trial in Ventura County where the shooting occurred.

King, 15, was openly gay, and McInerney's attorneys argued he made sexual advances to their client.

Ventura County prosecutors said McInerney targeted King because of his sexuality and they stressed that King, far from being the aggressor, was being bullied and was 30 pounds lighter than McInerney.

After nine weeks of trial, the jury failed to agree on a verdict against McInerney, with seven voting for manslaughter and five for murder, the Ventura County District Attorney's Office said.

There was no dispute during the trial that McInerney fired the fatal shots. The judge in that trial declared a mistrial in September due to the hung jury.

Ventura County prosecutors refiled the case and dropped the hate crime allegation. Prosecutors and McInerney's attorneys later reached a deal that had him plead guilty to second degree murder, voluntary manslaughter and use of a gun.

Attorneys for McInerney could not be immediately reached for comment.