Код: Выделить всё
package com.hks.DemoHib;
import java.util.Collection;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class App {
public static void main(String[] args) {
Configuration con = new Configuration().configure().addAnnotatedClass(Laptop.class).addAnnotatedClass(Alien.class);
SessionFactory sf = con.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Alien a1 = session.get(Alien.class, 1);
if (a1 == null) {
System.out.println("Alien with ID 1 not found!");
} else {
a1.setAname("Harsh");
System.out.println(a1.getAname());
Collection laps = a1.getLaps();
for (Laptop l : laps) {
System.out.println(l);
}
}
session.getTransaction().commit();
}
}
Alien.java
Код: Выделить всё
import javax.persistence.*;
import java.util.Collection;
@Entity
@Table(name = "Alien")
public class Alien {
@Id
private int aid;
private String aname;
@OneToMany(mappedBy = "alien")
private Collection laps;
// Getters and Setters
}
Код: Выделить всё
import javax.persistence.*;
@Entity
@Table(name = "Laptop")
public class Laptop {
@Id
private int lid;
private String lname;
@ManyToOne
@JoinColumn(name = "aid")
private Alien alien;
// Getters and Setters
}
Код: Выделить всё
org.hibernate.dialect.MySQLDialect
com.mysql.cj.jdbc.Driver
jdbc:mysql://localhost:3306/your_database
your_username
your_password
update
true
Столовый инопланетянин
помощь aname
1 Навин
2 Рахул
3 Маянк
Что могло каковы возможные причины этого и как я могу это решить?
Подробнее здесь: https://stackoverflow.com/questions/787 ... ing-record
Мобильная версия