hibernate.cfg.xml
<?xml version='1.0'/span> encoding='utf-8'/span>?
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"?
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<屬性名稱="connection.driver_class">org.postgresql.Driver</property>
<屬性名稱="connection.url">jdbc:postgresql:/localhost:5432/postgres</property>
<property name="connection.username">postgres</property>
<屬性名稱="connection.password">123456</property>
<屬性名稱="current_session_context_class"> thread</property>
<屬性名稱="show_sql">true</property>
<屬性名稱="format_sql">true</property>
<屬性名稱="hbm2ddl.auto">更新</property>。
<屬性名稱="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
</session-factory>
</hibernate-configuration>
SessionFactory
import org.hibernate.SessionFactory。
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private HibernateUtil()
{}
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure() .buildSessionFactory()。
} catch (Throwable ex) {
System.err.println("初始SessionFactory創建失敗。" ex)。
throw new ExceptionInInitializerError(ex)。
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory。
}
HumanBeing.java
@Entity
@Table(applyTo = "HumanBeing")
public class HumanBeing
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn (name="coordinates_id" )
private Coordinates坐標。
@Column(name = "creationDate")
private java.time.LocalDate createDate。
@Column(name = "realHero")
private boolean realHero。
@Column(name = "hasToothpick")
private boolean hasToothpick;
@Column(name = "impactSpeed")
private Float impactSpeed;
@Column(name = "weaponType")
@Enumerated(EnumType.STRING)
private WeaponType weaponType;
@Column(name = "mood")
@Enumerated(EnumType.STRING)
private Mood mood。
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn (name="car_id")
private Car汽車。
...
}
代碼
Session session = HibernateUtil.getSessionFactory() .openSession()。
Transaction tx1 = session.beginTransaction()。
session.save(new HumanBeing()。
tx1.commit()。
session.close()。
而錯誤:org.hibernate.MappingException。未知物體。HumanBeing
P.S: 看起來你的帖子主要是代碼,請增加一些細節。
看起來你的帖子主要是代碼,請增加一些細節。
看起來你的帖子主要是代碼,請增加一些細節。
看起來你的帖子主要是代碼,請增加一些細節。
看起來你的帖子主要是代碼,請增加一些細節。
uj5u.com熱心網友回復:
錯誤的原因是你的物體沒有與hibernate.cfg.xml
在你的hibernate.cfg.xml檔案中的<session-factory>中添加這個:
<mapping class="packagename.HumanBeing" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/327133.html
標籤:
