当前位置: 首页 > 知识库问答 >
问题:

hibernate不向数据库添加新记录

王叶五
2023-03-14
@Entity
@Table(name = "author")
public class Author implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "author_id")
    private Integer author_id;

    @Column(name = "author")
    private String author;  

    public Integer getAuthor_id() {
        return author_id;
    }

    public void setAuthor_id(Integer author_id) {
        this.author_id = author_id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}
@Repository
@Transactional
public class AuthorDAO {

    @Autowired
    private EntityManager entityManager;

    public void addNewAuthor(){
        Author newAuthor = new Author();
        newAuthor.setAuthor("Dan Brown");
        entityManager.persist(newAuthor);
    }
}
<persistence-unit name="engineerJPA" transaction-type="JPA">
  <class>com.engineering.pawel.entity.User</class>
  <class>com.engineering.pawel.entity.UserRole</class>
  <class>com.engineering.pawel.entity.Author</class>

  <jta-data-source>java:jboss/datasources/postgreSQL</jta-data-source>

  <properties>
   <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
   <property name="hibernate.max_fetch_depth" value="3" />
   <property name="hibernate.hbm2ddl.auto" value="update" />
   <property name="hibernate.show_sql" value="true" />
   <property name="hibernate.jdbc.batch_size" value="100" />
   <property name="hibernate.id.new_generator_mappings" value="true" />
   <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
   <property name="hibernate.transaction.auto_close_session" value="true" />
   <property name="javax.persistence.transactionType " value="jta" />
   <property name="hibernate.current_session_context_class" value="jta" />
   <property name="hibernate.connection.release_mode" value="auto" />
  </properties>
 </persistence-unit>
</persistence>
<context:annotation-config />
    <context:component-scan base-package="com.engineering.pawel" />

    <!-- Database configuration -->
    <tx:annotation-driven />

    <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:comp/env/persistence/emf"
        expected-type="javax.persistence.EntityManagerFactory" />

    <beans:bean id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <beans:property name="transactionManagerName" value="java:/TransactionManager" />
    </beans:bean>

    <beans:bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <beans:bean id="entityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <beans:property name="entityManagerFactory" ref="entityManagerFactory" />
    </beans:bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>
<spring.version>4.0.0.RELEASE</spring.version>
        <spring.security.version>3.2.5.RELEASE</spring.security.version>
        <hibernate-version>4.0.1.Final</hibernate-version>

我正在使用entityMenager登录,并没事。

@Repository
public class UserDAO{

    @Autowired
    private EntityManager entityManager;

    @SuppressWarnings("unchecked")
    public List<User> getAllUsers(){
        Query query = entityManager.createQuery("from User");
        List<User> listUsers = query.getResultList();
        return listUsers;
    }

    @SuppressWarnings("unchecked")
    public User findByUserName(String userName){

        List<User> users = new ArrayList<User>();

        Query query = entityManager.createQuery("from User where nick = :nick").setParameter("nick", userName);
        users = query.getResultList();

        if(users.size() > 0){
            return users.get(0);
        }else{
            return null;
        }
    }
}

共有1个答案

黎震博
2023-03-14

在hibernate文档中,您应该使用generationtype.sequence

实体

@Id
@SequenceGenerator(name = "author_id_seq", sequenceName = "author_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "author_id_seq")
@Column(name = "author_id")

PostgreSQL

CREATE SEQUENCE author_id_seq INCREMENT 1 START 1
 类似资料:
  • 问题内容: 我尝试更新我的插件。因此,我必须升级mysql_table。但是当尝试新列时,程序会获得异常。 这是我当前的表格: 现在,我向colum添加多一张桌子。我尝试更改表,一次工作,并添加一列,但又刷新一次,我收到此错误。 这是我的代码 这是我的错误 WordPress数据库错误:[重复的列名’say_state’] ALTER TABLE wp_customer_say添加了say_sta

  • 我无法在Firebase实时数据库中添加任何数据。数据库连接正常,但我无法添加任何数据。当我单击“到”按钮时,添加到数据库中的数据。 主要活动。java-代码 事件。java-代码

  • 问题内容: 我想向Firebase身份验证添加新数据,其中包含显示名称,电话号码,图像之类的数据。但是我想增加更多这样的性别,生日等等。是否可以添加新的? 问题答案: 无法将任意其他数据添加到Firebase身份验证用户配置文件。如果需要,请考虑使用Firebase Realtime数据库(或CloudFirestore)存储其他信息。 自几周前以来,您可以向Firebase身份验证用户配置文件中

  • 我想添加一个新的数据到firebase认证,它有像displayname,电话号码,图像的数据。但我想增加更多这样的性别,生日和更多。有可能添加新的吗?

  • 目前,我学习如何使用SpringBoot和访问多个数据库。在学习过程中,我遇到了向mysql表添加数据的问题。在我使用插入。。。。Value()要添加数据,当我使用get方法(SpringBoot)在网站上查找(显示)数据时,表仍然显示为null(无数据)。你们能就我的错误给我一些建议吗。 额外信息:我把mysql表放在SpringBoot的资源文件夹中。当我使用get方法(Springboot的

  • 问题内容: 我正在尝试向Kettle添加新的数据类型(几何)。我在org.pentaho.di.compatibility中添加了一个新的Value类型。我添加了ValueGeometry类,并对ValueInterface和Value进行了必要的修改。代码会编译,但是新数据类型不会显示在Select之类的插件中。我在这里想念什么?如果您能指出这些插件的源代码,我也将不胜感激。 谢谢。 问题答案: